Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Chaining

 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Have run below code:

When the exception array index out of bound is raised,control goes to Catch where exception has been displayed by SOP(e)
part.
After it,next exception that arise is divide y zero.
My question is when i run,only arithmatic exception is displayed though its run after SOP of e is done
Shouldn't the result be below which comes when i separate two line

catch(Exception e)
{ int a2=10;
//System.out.println("Exception e"+e);
System.out.println(e );
System.out.println(a2/0);
}
*****************
java.lang.ArrayIndexOutOfBoundsException: 12
Exception in thread "main" java.lang.ArithmeticException: / by zero
at collect.exception.main(exception.java:23)


Why only one exception is displayed in below case
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.out.println(); statement is not atomic. Go through its constituent parts with a pencil and paper, and write down the execution of each little part in order. Remember the precedences of the operators in that statement. Then you can see when and how that call completes.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The e + "" + a2/0 part is evaluated before the System.out.println method is executed.
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and campbell,

Have got it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Rob said earlier, the events occur in something like this order
  • Find the System class
  • Find its "out" member.
  • Find "out"'s println method.
  • Find the Exception, and its toString() method
  • Get the String to catenate with . . .
  • . . . a2 / 0
  • Print that lot out.
  • Return to "out"
  • Return to System
  • Return to the catch block
  • Go on from the catch block to the next bit of code.



  • At least that's what supposed to happen. When you get to a2 / 0, you suffer an ArithmeticException, which goes back to out, back to System, back to the catch (not caught because it didn't originate in a try), back to the method, and if it doesn't find an appropriate catch, it keeps going until it stops which ever thread it is running in.

    So you see, the execution stops where the appears above. That is why all record of your ArrayIndexOutOfBoundsException vanishes.
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Very good Explaination...
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50268
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shanky Sohar wrote:Very good Explaination...
    Thank you. I hope the original poster has seen it too.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic