Select the correct answerWhat is the output of the Java code with FINALLY block and RETURN statement?public class ExceptionTest6 { static void show() { try { System.out.println("inside TRY"); return; } finally { System.out.println("inside FINALLY"); } } public static void main(String[] args) { show(); }}Optionsinside TRYinside FINALLYCompiler errorinside TRYinside FINALLY
Question
Select the correct answerWhat is the output of the Java code with FINALLY block and RETURN statement?public class ExceptionTest6 { static void show() { try { System.out.println("inside TRY"); return; } finally { System.out.println("inside FINALLY"); } } public static void main(String[] args) { show(); }}Optionsinside TRYinside FINALLYCompiler errorinside TRYinside FINALLY
Solution
The correct answer is "inside TRY" followed by "inside FINALLY".
Here's the step by step explanation:
- The
show()method is called from themainmethod. - Inside the
show()method, the code inside thetryblock is executed first. It prints "inside TRY" and then encounters areturnstatement. - Normally, a
returnstatement would end the method execution and control would go back to the calling method. However, if afinallyblock is present, it gets executed before the method ends, no matter what. - So, after the
tryblock, thefinallyblock is executed and "inside FINALLY" is printed. - After the
finallyblock, the method ends and control goes back to themainmethod.
So, the output of the code will be:
inside TRY
inside FINALLY
Similar Questions
redict the output of following Java programclass Test extends Exception {}class Main { public static void main(String args[]) { try { throw new Test(); } catch (Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } }}OptionsGot the Test Exception Inside finally blockInside finally blockCompiler ErrorGot the Test Exception
What will be the output of the following code?class Test extends Exception { }public class Main { public static void main(String args[]) { try { throw new Test(); } catch(Test t) { System.out.print("Catch the Test Exception,"); } finally { System.out.print("Inside finally block "); } }}Options: Pick one correct answer from belowCatch the Test Exception,Inside finally blockRuntime errorCatch the Test Exception, Inside the finally block
Predict the output of following Java programclass Test extends Exception {}class Main { public static void main(String args[]) { try { throw new Test(); } catch (Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } }}OptionsGot the Test ExceptionGot the Test Exception Inside finally blockInside finally blockCompiler Error
What is the output of the following Java code:Code:class HackerEarth { int getValue() { int returnValue = 10; try { String[] Languages = { "Try block", "Try block is running" }; System.out.println(Languages[1]); } catch (Exception e) { System.out.println("Catch Block :" + returnValue); return returnValue; } finally { returnValue += 10; System.out.println("Finally Block :" + returnValue); } return returnValue; } public static void main(String args[]) { HackerEarth var = new HackerEarth(); System.out.println("Main Block:" + var.getValue()); }}
Select the correct answerWhat is the purpose of the finally block in Java exception handling?OptionsTo handle an exceptionTo clean up resources after a try blockNone of theseTo catch an exception
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.