Exceptions

Q1: What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print('A'); 
        }  
        catch (Exception ex) 
        {
            System.out.print('B');  
        } 
        finally 
        {
            System.out.print('C'); 
        } 
        System.out.print('D'); 
    }  
    public static void badMethod() 
    {
        throw new Error(); /* Line 22 */
    } 
}

A C is printed before exiting with an error message.

B BC is printed before exiting with an error message.

C Compilation fails.

D ABCD

ANS:A - ABCD

Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread 'main' java.lang.Error).



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.