• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joanne Neal..
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joanne.
 
reply
    Bookmark Topic Watch Topic
  • New Topic