Challenge Statement
Let us take two codes as shown asSample.java
and Demo.java
What would be the output in both the cases?
Right Answer
Sample.java: no output.
Demo.java:I am still running.
Reasoning
Check this out
Right Answer Given By
Meet Mr. Jagannath Kundu | |
|
Meet Mr. Sandip Subuddhi |
|
Meet Mr. Suryans Goel |
OUTPUT FOR 1st case: The value 5 will be stored in k, and the program will terminate, getting System.exit(0).
ReplyDeleteOUTPUT FOR 2nd case: ArithmeticException will be thrown at k=i/j. Divide by zero exception occurs. Program control goes to finally block, displaying "I am still running".
Sample.Java:- no output. because of no execption arise in this program then next line system.exit(0) is executed that's means terminate the program.
ReplyDeleteDemo.java:- I am still running. because of this program arise the execption and execution jump to catch block but when we write return statements and finally both are same program finally block execute first then return statement. There is no return because main method is return type void.
Sample.Java:- no output. because of no execption arise in this program then next line system.exit(0) is executed that's means terminate the program.
ReplyDeleteDemo.java:- I am still running. because of this program arise the execption and execution jump to catch block but when we write return statements and finally both are same program finally block execute first then return statement. There is no return because main method is return type void.
Sample.Java:- no output. because of no execption arise in this program then next line system.exit(0) is executed that's means terminate the program.
ReplyDeleteDemo.java:- I am still running. because of this program arise the execption and execution jump to catch block but when we write return statements and finally both are same program finally block execute first then return statement. There is no return because main method is return type void.
Sample.Java:- no output. because of no execption arise in this program then next line system.exit(0) is executed that's means terminate the program.
ReplyDeleteDemo.java:- I am still running. because of this program arise the execption and execution jump to catch block but when we write return statements and finally both are same program finally block execute first then return statement. There is no return because main method is return type void.
Sample.java: no output. Becaus no exception will arise as 25/5 is valid operation. Then next line will be executed as System.exit(0) and it will terminate the program.
ReplyDeleteDemo.java: output will be - I am still running. Here 25/0 is an invalid arithmetic operation so exception will arise. The exception will be ArithmeticException. So now it will jump to catch statement and will not execute the line mentioning System.exit(0). After all of these finally block will be executed and print the output.
No output in both e cases.
ReplyDeleteBoth the cases no output... Sir
ReplyDeleteSample prints nothing because no exception so "System.exit(0)" is executed which immediately terminates program and program doesn't reach finally block
ReplyDeleteDemo: "I am still running"
Because exception arises and catch block executes return but finally block is executed before"return" and thus the printing.