首頁> Java> java教程> 主體

Deep Dive into Java Exceptions

PHPz
發布: 2024-08-19 06:01:02
原創
352 人瀏覽過

Java Core Exception Class

hierarchy of exception classes: Throwable < Error < Exception where the Throwable is the superclass

Throwable

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. reference

In practice, Throwable is not typically used directly by developers. Instead, it serves as the foundation for its two direct subclasses: Error and Exception.

try { // some process } catch (Throwable t) { throw new Throwable(t); }
登入後複製

Error

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Errors typically represent abnormal conditions that occur in the JVM itself reference

An abnormal condition means that the problem typically arises from factors outside the control of the application and is generally unrecoverable. Ex : OutOfMemoryError, StackOverflowError

Deep Dive into Java Exceptions

Exception

Exception refers to an unexpected event or condition that occurs during the execution of a program, and we should try catch. By catching exceptions we should be able to handle unexpected situations gracefully, ensuring that the program doesn't crash.

int[] arr = {1, 2, 3}; try { System.out.println(arr[3]); // This will throw ArrayIndexOutOfBoundsException } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Unchecked Exception: " + e.getMessage()); }
登入後複製

Types of Exceptions

There are 2 types of exception in java. reference

Checked Exceptions

Checked exception is like a situation that you know might go wrong, so you're required to plan for it. They must either be caught using a try-catch block or be declared in the method signature with a throws clause. If a method can throw a checked exception and you don't handle it, the program will not compile.

// Checked exception try { readFile("nonexistent.txt"); } catch (FileNotFoundException e) { System.out.println("Checked Exception: " + e.getMessage()); }
登入後複製
// Checked exception public void getData() throws SQLException { // throw SQLException throw new SQLException("err"); }
登入後複製

Unchecked Exception

Unchecked exceptions, aka. runtime exceptions, are exceptions that the Java compiler does not require you to handle. They are subclasses of RuntimeException. Unlike checked exceptions, these exceptions do not need to be caught or declared in the method signature. They usually indicate programming errors, such as logic flaws, incorrect use of APIs, or violations of assumptions in the code.

String text = null; System.out.println(text.length()); // This will throw a NullPointerException
登入後複製

Common Types of Java Exceptions

NullPointerException: Occurs when an application tries to use an object reference that has not been initialized.

String text = null; // text is not initialized try { System.out.println(text.length()); // Attempting to call length() on a null reference } catch (NullPointerException e) { System.out.println("Caught a NullPointerException: " + e.getMessage()); }
登入後複製

ArrayIndexOutOfBoundsException: Thrown when an attempt is made to access an array with an illegal index.

int[] numbers = {1, 2, 3}; try { int value = numbers[5]; // Attempting to access index 5, which is out of bounds } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Caught an ArrayIndexOutOfBoundsException: " + e.getMessage()); }
登入後複製

IllegalArgumentException: Thrown when a method receives an inappropriate argument.

public class IllegalArgumentExample { public static void setAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); // Illegal argument } System.out.println("Age set to: " + age); } public static void main(String[] args) { try { setAge(-5); // Passing a negative age, which is illegal } catch (IllegalArgumentException e) { System.out.println("Caught an IllegalArgumentException: " + e.getMessage()); } } }
登入後複製

How To Handle Exceptions

Try-Catch

Use try-catch when you want to handle specific exceptions that might be thrown within a block of code

try { int result = 10 / 0; // This will throw ArithmeticException } catch (ArithmeticException e) { System.out.println("Caught an ArithmeticException: " + e.getMessage()); }
登入後複製

Multi-Catch

Use multi-catch when you want to handle multiple exception types in a similar manner

try { String str = null; System.out.println(str.length()); // This will throw NullPointerException } catch (NullPointerException | ArrayIndexOutOfBoundsException e) { System.out.println("Caught an exception: " + e.getMessage()); }
登入後複製

Try-With-Resources

Use try-with-resources when you are working with resources that need to be closed after use, such as files, sockets, or database connections.

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Caught an IOException: " + e.getMessage()); }
登入後複製

Finally

Use the finally block when you need to ensure that certain code executes regardless of whether an exception was thrown or not

try { file = new FileReader("file.txt"); } catch (IOException e) { System.out.println("Caught an IOException: " + e.getMessage()); } finally { if (file != null) { file.close(); // Ensure the file is closed } }
登入後複製

Best Practices for Exception Handling

Do Not Ignore Exceptions: exceptions should be handled appropriately, not just caught and ignored.

try { file = new FileReader("file.txt"); } catch (IOException ignored) { // ignored }
登入後複製

Use Specific Exceptions: use specific exceptions rather than using generic ones.

try { // Code that may throw exceptions String text = null; text.length(); } catch (Exception e) { // Too broad; will catch all exceptions System.err.println("An error occurred: " + e.getMessage()); }
登入後複製

proper way to handle it:

try { // Code that may throw exceptions String text = null; text.length(); } catch (NullPointerException e) { // Handle specific exception System.err.println("Null pointer exception: " + e.getMessage()); } catch (Exception e) { // Handle other exceptions System.err.println("An error occurred: " + e.getMessage()); }
登入後複製

Clean Resource Handling: always close resources to avoid memory leaks

FileReader fileReader = null; try { fileReader = new FileReader("file.txt"); // Read from the file } catch (IOException e) { System.err.println("File not found: " + e.getMessage()); } finally { fileReader.close(); // clouse resources }
登入後複製

Custom Exceptions: create custom exceptions when the standard exceptions don’t suit the specific error conditions.

// Custom Exception public class CustomException extends Exception { public CustomException(String message) { super(message); } } // Usage public class Example { public void performOperation() throws CustomException { // Some condition throw new CustomException("Custom error message"); } public static void main(String[] args) { Example example = new Example(); try { example.performOperation(); } catch (CustomException e) { System.err.println("Caught custom exception: " + e.getMessage()); } } }
登入後複製

Logging: log exceptions for debugging and maintenance.

public class Example { private static final Logger logger = Logger.getLogger(Example.class.getName()); public void riskyMethod() { try { // Code that may throw an exception int result = 10 / 0; } catch (ArithmeticException e) { // Log the exception logger.severe("An arithmetic error occurred: " + e.getMessage()); } } }
登入後複製

Avoid Overusing Exceptions: Warn against using exceptions for controlling flow; they should only be used for handling truly exceptional situations.

public class Example { public void process(int[] array) { try { // Using exceptions to control flow if (array.length < 5) { throw new ArrayIndexOutOfBoundsException("Array too small"); } // Process array } catch (ArrayIndexOutOfBoundsException e) { // Handle exception System.out.println("Handled array size issue."); } } }
登入後複製

proper way to handle it:

public class Example { public void process(int[] array) { if (array.length >= 5) { // Process array } else { // Handle the condition without using exceptions System.out.println("Array is too small."); } } }


any feedback will be helpful :)

以上是Deep Dive into Java Exceptions的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!