Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dharmendra gahlot
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne Neal..
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic