Java Programming - Exceptions - Discussion

Discussion :: Exceptions - Pointing out the correct statements (Q.No.2)

2. 

public class MyProgram 
{
    public static void throwit() 
    {
        throw new RuntimeException();
    }
    public static void main(String args[])
    {
        try 
        {
            System.out.println("Hello world ");
            throwit();
            System.out.println("Done with try block ");
        }
        finally 
        {
            System.out.println("Finally executing ");
        }
    }
}
which answer most closely indicates the behavior of the program?

[A]. The program will not compile.
[B]. The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
[C]. The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
[D]. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

Answer: Option D

Explanation:

Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated.


Prashanta said: (Nov 23, 2011)  
I think the answer is C. Finally block will be all time executed whether catch block are belong there or not. but it it will executed at the last time.

Sundar said: (Dec 14, 2011)  
The given answer is correct. I have tested it.

D:\Java>javac Test.java

D:\Java>java Test
Hello world
Finally executing
Exception in thread "main" java.lang.RuntimeException
at Test.throwit(Test.java:5)
at Test.main(Test.java:12)


Hope this will help you. Have a nice day!

Saroj Das said: (Mar 9, 2012)  
I think answer is C


Hello world
Exception in thread "main" java.lang.RuntimeException
at MyProgram.throwit(MyProgram.java:6)
at MyProgram.main(MyProgram.java:13)
Finally executing

Alis said: (Sep 15, 2013)  
I don't think it's predictable.

I ran the class a number of times and have seen different order of expressions in standard output.

Archana said: (Jan 2, 2015)  
Hello world.

Exception in thread "main" java.lang.RuntimeException.

Finally executing:

at MyProgram.throwit(MyProgram.java:5).
at MyProgram.main(MyProgram.java:12).

Java Result: 1.

And after executing second time o/p is :

Hello world.

Finally executing.

Exception in thread "main" java.lang.RuntimeException.

at MyProgram.throwit(MyProgram.java:5).
at MyProgram.main(MyProgram.java:12).

Java Result: 1.

Output is not predictable.

Sai Prasad said: (Jul 25, 2016)  
Hello world

Exception in thread "main" java.lang.RuntimeException.

Finally executing
at javaapplication8.JavaApplication8.throwit(JavaApplication8.java:8)
at javaapplication8.JavaApplication8.main(JavaApplication8.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 sec)

Sasikanta said: (Nov 15, 2017)  
I am getting below error at line 5 i.e " throw new RuntimeException();"

No exception of type RuntimeException can be thrown; an exception type must be a subclass of Throwable.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.