0

I know what are the output. But question is what is the explanation of that out put.

 public class LongExp{
  LongExp() throws Exception{
    LongExp.start();
  }
  public static void start()throws RuntimeException{
    throw new IllegalMonitorStateException();
  }
  public static void main(String args[]) throws Throwable{
    try{
      try{
        try{
          new LongExp();
        } catch(Throwable t){

          System.out.println("catch(Throwable t) 1" );
          throw t;
        }
      }catch(Throwable t){
          System.out.println("catch(Throwable t) 2" );
        if (t instanceof IllegalMonitorStateException){
             System.out.println("(t instanceof IllegalMonitorStateException)" );
          throw (RuntimeException)t;
        }else{
             System.out.println("else (t instanceof IllegalMonitorStateException)" );
          throw (IllegalMonitorStateException)t;
        }
      }
    }catch(IllegalMonitorStateException e){

      System.out.println("a" );
    }catch(RuntimeException e){

      System.out.println("b" );
    }catch(Exception e){
      System.out.println("c" );
    }catch(Throwable e){
      System.out.println("d" );
    }
  }
}

This are the output

catch(Throwable t) 1
catch(Throwable t) 2
(t instanceof IllegalMonitorStateException)
a

My explanation what ever is being propagated by the type conversion, It was just the change of the reference but not the instance type of that exception-object. That is why at the end It caught the IllegalMonitorStateException .

Am I correct ?

EDIT:TYPOS;

2 Answers2

1

Yes,it is run time polymorphism,you can assign object of child class to the parent class reference type.

Here you are assigning IllegalMonitorStateException to different parent class's (Throwable,RuntimeException),which contains the instance of IllegalMonitorStateException(child class).

The reason why you have got the IllegalMonitorStateException at the end is,it was the originally propagated object,and according to the exception handling rule,it will be caught by most specific catch block first than generic one.

dReAmEr
  • 6,986
  • 7
  • 36
  • 63
0

Yes, to answer your question.

And, Why

because we can always assign the child class reference IllegalMonitorStateException in your case to the parent class. Also, when we rethrow an exception, let say E1 - everything about this E1 is preseved even when it is caught by the catch blocks in the higher hierarchy.

You can verify this by printing the stack trace in the outer catch blocks. For example,

class Exception1 {

  Exception1() throws Exception{
    Exception1.start();
  }
  public static void start()throws RuntimeException{
    throw new IllegalMonitorStateException();
  }
  public static void main(String args[]) throws Throwable{
    try{
      try{
        try{
          new Exception1();
        } catch(Throwable t){
          t.printStackTrace();
          throw t;
        }
      } catch(Throwable t){
          t.printStackTrace();
          throw t;
       }
    } catch(IllegalMonitorStateException e){
        e.printStackTrace();
    }
  }
}

All three stacks will print the same description.

Tirath
  • 2,294
  • 18
  • 27