Can we catch more than one exception in a single catch block?

Default exception handling in Java refers to the mechanism by which the Java runtime (JVM) deals with uncaught exceptions that propagate through the call stack without being intercepted by a catch block.

Default Exception Handling Process

  1. Exception Occurs:
  2. An exception is thrown during the execution of a program, and if it's not caught within the method where it occurs, it propagates up to the caller.
  3. Propagation:
  4. The exception continues to propagate up the call stack from one method to its caller. Each method in the call stack is checked to see if it has a catch block that handles the exception type.
  5. Top-Level Handling:
  6. If the exception reaches the main method and is not handled, it will continue to propagate to the JVM.
  7. JVM Response:
  8. If the exception is still not caught by any catch block and has reached the top level (i.e., the JVM), the Java Virtual Machine (JVM) handles it by terminating the program. The JVM prints an error message and a stack trace to the standard error stream.

Example of Default Exception Handling

Here is an example where an exception is not caught explicitly by any catch blocks:

public class DefaultExceptionHandlingExample {
    public static void main(String[] args) {
        methodA();
    }

public static void methodA() {
methodB();
}

public static void methodB() {
int result = 10 / 0; // This will throw ArithmeticException
}


}  

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at DefaultExceptionHandlingExample.methodB(DefaultExceptionHandlingExample.java:10)
    at DefaultExceptionHandlingExample.methodA(DefaultExceptionHandlingExample.java:6)
    at DefaultExceptionHandlingExample.main(DefaultExceptionHandlingExample.java:3)

Setting a Default Uncaught Exception Handler

You can set a default uncaught exception handler to catch and handle exceptions that were not caught elsewhere:

public class CustomExceptionHandlerExample {
    public static void main(String[] args) {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("Unhandled exception: " + e.getMessage());
            }
        });

methodA();

}

public static void methodA() {
methodB();
}

public static void methodB() {
int result = 10 / 0; // This will throw ArithmeticException
}


}  

Output:

Unhandled exception: / by zero


Follow-up Question

  1. Why is it considered a good practice to handle exceptions explicitly rather than relying on default exception handling?
  2. Answer: Handling exceptions explicitly allows for more controlled and graceful recovery or shutdown of the application, providing better user experience and preserving system integrity and data consistency. Explicit handling also offers opportunities for logging errors comprehensively, notifying users or administrators, and taking corrective actions if possible.