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

Long Answer
Views 36

Answer:

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

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.