Home / Questions / What happens if a try-catch-finally statement does not have a catch clause to handle an exception thrown within the try block?
Explanatory Question

What happens if a try-catch-finally statement does not have a catch clause to handle an exception thrown within the try block?

👁 37 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

If a try block throws an exception and there is no suitable catch clause to handle it, the exception is propagated upward to the next higher-level try-catch block (if present).
If no such block exists, the program terminates abnormally and displays an error message.

Example:


try {
    int a = 10 / 0;
}
finally {
    System.out.println("Finally executed before program ends.");
}

Output:


Finally executed before program ends.
Exception in thread "main" java.lang.ArithmeticException: / by zero