|
| 1 | +When using multiple catch blocks, it's important to order them from most specific to most general exceptions. |
| 2 | +If a more general exception is caught first, it will catch all of its subclasses, preventing more specific catch blocks |
| 3 | +from being reached. |
| 4 | + |
| 5 | +This is why we typically see catch blocks for specific exceptions like NullPointerException before a catch block for the general Exception class. |
| 6 | + |
| 7 | +The 'throws' clause in a method signature is used to declare that the method might throw certain checked exceptions. |
| 8 | +It doesn't actually throw or handle the exception, but rather informs the caller that they need to either catch these |
| 9 | +exceptions or declare them in their own method signature. |
| 10 | + |
| 11 | +This is part of Java's checked exception mechanism, ensuring that potential exceptions are dealt with at compile-time. |
| 12 | + |
| 13 | +To create a custom checked exception, you should extend the Exception class. Extending RuntimeException would create an |
| 14 | +unchecked exception. Extending Error is not appropriate for application-level exceptions, and Throwable is too general. |
| 15 | + |
| 16 | +By extending Exception, you create a checked exception that must be either caught or declared in the method signature, |
| 17 | +enforcing proper exception handling at compile-time. |
| 18 | + |
| 19 | +The main advantage of try-with-resources is that it automatically closes resources that implement the AutoCloseable |
| 20 | +interface. This feature, introduced in Java 7, simplifies resource management and helps prevent resource leaks. |
| 21 | + |
| 22 | +It ensures that resources are closed in the correct order and handles exceptions that might occur during closing. |
| 23 | + |
| 24 | +This leads to cleaner, more reliable code compared to manually closing resources in a finally block. |
| 25 | + |
| 26 | +When an exception is not caught in a method, it is propagated up the call stack to the calling method. |
| 27 | +This process continues until the exception is either caught and handled or reaches the top of the call stack. |
| 28 | + |
| 29 | +If it reaches the top without being caught, the default exception handler prints the stack trace and terminates the program. |
| 30 | + |
| 31 | +This mechanism allows for centralized exception handling and gives flexibility in where exceptions are handled. |
| 32 | + |
| 33 | +The addSuppressed() method is used to add a suppressed exception in Java. This method was introduced along with the |
| 34 | +try-with-resources statement in Java 7. |
| 35 | + |
| 36 | +When an exception occurs in the try block and another exception occurs while closing resources in the implicit finally |
| 37 | +block, the latter exception is suppressed and added to the original exception. |
| 38 | + |
| 39 | +This allows for preserving information about multiple exceptions that occurred during the execution of a block of code. |
| 40 | + |
| 41 | +You can retrieve suppressed exceptions using the getSuppressed() method. |
0 commit comments