File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception Chaining" Watch "Exception Chaining" New topic
Author

Exception Chaining

isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

The e + "" + a2/0 part is evaluated before the System.out.println method is executed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Thanks Rob and campbell,

Have got it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

    Joined: Mar 17, 2010
    Posts: 1051

    Very good Explaination...


    SCJP6.0,My blog Ranchers from Delhi
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36508
        
      16
    Shanky Sohar wrote:Very good Explaination...
    Thank you. I hope the original poster has seen it too.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exception Chaining
     
    Similar Threads
    problem with oops method::
    Why so many Exception Handling classes ?
    Reg. Exceptions in finally block
    When does the JVM exit?
    Read file from directory, update contents of the each file