(2) Runtime Exceptions - Part II

   There are a few more important things to cover in runtime exceptions,explaining them using example questions would give a proper understanding.
  • Q)
public class ExceptionEg {

    static void m(){
        try {
            throw new RuntimeException();
        } catch (NullPointerException e) {
           
        } finally {
            System.out.print("Hey");
        }
    }
    public static void main(String[] args) {
        try {
            m();
        } catch (IllegalArgumentException e) {
            }
        }
}

What is the output?
a)prints Hey followed by an exception report
b)prints an exception report followed by Hey
c)prints only Hey
d)compilation fails

At method m,a runtimeException is thrown,but the relevant catch block cannot catch the thrown exception.(Because RuntimeException > NullPointerException).So the runtime exception would be propagated to m method's caller which is the main method.Besides that,since try block was executed,its relevant finally block should definitely executed.So "Hey" would be printed.

Now the exception is at the main methods level.But even its catch block cannot catch this exception because IllegalArgumentException is a subclass of RuntimeException (if it was other way round,it could've caught)

But will a compilation error ocurr? No,because the not handle exception is a runtime exception and hence code will compile fine and would give an exception at runtime.
So the answer is: (a) 

  •  Q)
public class A{
    public void method1() {
        B b=new B();
        b.method2();
        System.out.print("A");
    }
}

public class B{
    public void method2(){
        throw new NullPointerException();
    }
}

public class ExceptionEg {

    public static void main(String[] args) {
        try {
            A a=new A();
            a.method1();
          
        } catch (Exception e) {
            System.out.print("an error occured");
    }
      
    }
}

 
 Would "A" and "an error occured" print?
 Here the exception would be propagated to a.method1() line.Then the relevant catch block would catch the exception and print "an error occured".
But "A" would not be printed because at the moment an exception occured,the rest of the code would not run and immediately find a relevant catch block (but if there were a finally block it would run before - we discussed about it in the part I)

  • Q)
public class ExceptionEg {
   
    static void test() throws RuntimeException{
        try {
            System.out.print("test");
            throw new RuntimeException();
        } catch (Exception e) {
            System.out.print("exception");
        }
    }

    public static void main(String[] args) {
        try {
            test();
           
        } catch (RuntimeException e) {
            System.out.print("an error occured");
    }System.out.print("end");
       
    }
}


 What would be the output?
First of all,you should notice this,
 Having nets of not matching way would not lead to a compilation failure because these two nets(catch blocks) are in different try blocks.
Immediate matching catch block(Exception e) will catch it.
Since those are runtime exceptions (or even if they were checked exceptions,since there are handled in try - catch block) program would not crash and hence,even the "end" would be printed after catching the exception.

so the answer is:

  "test exception end"
 



 
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment