| Author |
not able to understand output of the program related to exception handling?
|
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 892
|
|
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 ?
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
dharmendra gahlot
Ranch Hand
Joined: Apr 22, 2009
Posts: 52
|
|
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: 1778
|
|
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: 3011
|
|
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: 52
|
|
|
Thanks Joanne Neal..
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
|
Thanks Joanne.
|
 |
 |
|
|
subject: not able to understand output of the program related to exception handling?
|
|
|