Output of Java program | Set 12(Exception Handling)
Prerequisites : Exception handling , control flow in try-catch-finally
1) What is the output of the following program?
Explanation: Once an exception occurs in try block, the execution passes to corresponding catch statement and doesn’t return back to try block. Only one of the catch blocks are executed at a time. finally block is always executed whether or not the exception occurred.
2) What is the output of the following program?
Explanation: If an exception is handled in the catch statement, the program continues with its normal execution, after executing the catch statement corresponding to that exception. Also, when an exception occurs in the try block, the rest of the program in the try block is not executed.
3) What is the output of the following program?
Explanation: The only case when the code inside finally block is not executed is when System.exit(0) is called explicitly in the program. Then exit statement is called and the program is terminated without executing any further.
4) What is the output of the following program?
a) Compilation error
b) Runtime error
Explanation: Exceptions can be thrown in catch clause. This is done in order to change the exception type at run time. Exceptions in catch clause are thrown by creating instances of class Throwable as shown in the program.
5) What is the output of the following program?
Explanation: In multi-catch statements, the exceptions must be listed from more specific to more general. Only one catch statement which is most specific to the occurred exception is executed.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...