wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes not able to understand output of the program related to exception handling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "not able to understand output of the program related to exception handling?" Watch "not able to understand output of the program related to exception handling?" New topic
Author

not able to understand output of the program related to exception handling?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

please consider following program




when i run this program it gives the output as :

m in try block
Exception in thread "main" java.lang.ClassCastException // why does this prints before printing next line i.e. before array index out of bounds
array index out of bounds
i am always executed
at Dump39.go(Dump39.java:30)
at Dump39.main(Dump39.java:17)
Java Result: 1


when ArrayIndexOutOfBoundsException occurs the control goes to first catch block so it must first print "array index out of bounds" and then should throw ClassCastException. but it happens opposite. please help ?
dharmendra gahlot
Ranch Hand

Joined: Apr 22, 2009
Posts: 54
Program output is as below with Java 5 and Java 6.

m in try block
array index out of bounds
i am always executed
Exception in thread "main" java.lang.ClassCastException
at Dump39.go(Dump39.java:21)
at Dump39.main(Dump39.java:8)

Dharmendra Gahlot
SCJP-5.0 87%,SCWCD-5.0 92%
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Are you using an IDE like Eclipse? I can see the difference when running in Eclipse and cmd prompt.

Eclipse output:

cmd prompt output:


I have experienced this previously in Eclipse.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Hope this helps.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3412
    
  12
Your System.out.println calls are written to standard output.
The Exception in thread "main" java.lang.ClassCastException message is written to standard error.
Because you are writing to different output streams and the i/o is asynchronous there is no guarantee of the order in which the messages will be written.
If you want to guarantee the order, change the System.out.println calls to System.err.println calls


Joanne
dharmendra gahlot
Ranch Hand

Joined: Apr 22, 2009
Posts: 54
Thanks Joanne Neal..
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Joanne.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not able to understand output of the program related to exception handling?
 
Similar Threads
Exam Objectives Flow Control.
exam objective 4.3 cmd-line args to main
exception..try catch block execution
Why finally block is not executed completely?
Exception throwing