Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 5)
5.
What will be the output of the program?
public class RTExcept 
{
    public static void throwit () 
    {
        System.out.print("throwit ");
        throw new RuntimeException();
    }
    public static void main(String [] args) 
    {
        try 
        {
            System.out.print("hello ");
            throwit();
        }
        catch (Exception re ) 
        {
            System.out.print("caught ");
        }
        finally 
        {
            System.out.print("finally ");
        }
        System.out.println("after ");
    }
}
hello throwit caught
Compilation fails
hello throwit RuntimeException caught after
hello throwit caught finally after
Answer: Option
Explanation:

The main() method properly catches and handles the RuntimeException in the catch block, finally runs (as it always does), and then the code returns to normal.

A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing.

Discussion:
9 comments Page 1 of 1.

Nikshitha said:   7 years ago
But after finally block, if any statement exists does it executes?

As per my knowledge, I think it should not execute.

Seonab said:   8 years ago
I have a question on above code. I think the static method need to be execute first.

Seonab said:   8 years ago
Class is the Parent class of Exception class.
(1)

Wojtek said:   10 years ago
Why we do not need to statement 'throws' RuntTimeEx?
(1)

Sree said:   1 decade ago
If we have normal static method and public static void main() method, First preference is given to static main method.
(1)

Prasad said:   1 decade ago
I have a question on above code. I think the static method need to be execute first.
(1)

Anurag singh said:   1 decade ago
Hi, Exception class is parent class of RunTimeException.

Azam said:   1 decade ago
Sorry, but RuntimeException class is the Parent class of Exception class. So it can be handled by exception class.

Mudit said:   1 decade ago
Since RuntimeException is not a subclass of Exception class and neither it needs to be declared or handle then how can a catch clause with Exception object as argument can handle this Runtime Exception?

Post your comments here:

Your comments will be displayed after verification.