Java Programming - Exceptions - Discussion

Discussion Forum : 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?
The program will not compile.
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.
The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
Answer: Option
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.

Discussion:
7 comments Page 1 of 1.

Sasikanta said:   8 years ago
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.

Sai prasad said:   9 years ago
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)

Archana said:   1 decade ago
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.

Alis said:   1 decade ago
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.

Saroj das said:   1 decade ago
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

Sundar said:   1 decade ago
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!

Prashanta said:   1 decade ago
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.

Post your comments here:

Your comments will be displayed after verification.