• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Surya Bavirti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i identify CheckedExceptions&UncheckedExceptions
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Java Tutorials! There you find that unchecked Exceptions have RuntimeException (or Error) as a superclass, and checked Exceptions don't have RuntimeException (or Error) as a superclass.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would a programmer want to check this?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler insists checked Exceptions be declared or caught, but it doesn't insist for unchecked Exceptions.
Because most unchecked Exceptions require corrections to the code if they occur.
 
Surya Bavirti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By seeing anexception, how can i identify wether the exception is a super class of RuntimeException? or not?i'm new to to Java
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I mean the last query Surya has posted.. why would Surya want to check this?

mmm.. sorry messed up..

I guessed that Surya wanted to check whether a Throwable object was of type Exception or Error or RuntimeException. What is the use of that to a programmer?
[ September 29, 2008: Message edited by: Gamini Sirisena ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't usually test what type an Exception is; you usually try to anticipate it (if there is a checked Exception notified you must anticipate it). The throws clause of any methods you call or its Javadoc or both should tell you what sort of Exceptions it throws, and you set up appropriate catch blocks for those types, always starting with the most specific (subtype) and ending with the most general (supertype).

If you insist on testing the type of a specific Exception object you can use the instanceof operatorbut I have never seen anything like that written, nor would I regard that as good programming.
 
Surya Bavirti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Sirisena & Campbell.now i got an idea about Exceptions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic