This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question?" Watch "Exception question?" New topic
Author

Exception question?

Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
How do you know that an Exception is a Runtime Exception , Not a checked exception?

Are all the exceptions below runtime exception or error which do not need handle or declare?

(Exceptions in K&B book chapter 5 table 5-2)

ArrayIndecOutofBoundsException
ClassCastException
IllegalArgumentException
IllegalStateException
NullPointerException
NumberFormatException
AssertionError
ExceptionInInitializerError
StackOverflowError
NoClassDefFoundError

Thanks a lot.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Anna-

If the exception descends from java.lang.RuntimeException (like NullPointerException), then it's unchecked and you don't have to catch it. In fact, most of the time you shouldn't catch these kinds because they indicate an error in your program that should be resolved (for instance, your program should check for null).

If the exception descends from java.lang.Exception, without having java.lang.RuntimeException in its hierarchy (like java.io.IOException), then it's checked and you have to catch it.

The last category is errors. These descend from java.lang.Error and indicate serious problems (like java.lang.OutOfMemoryError). These also do not have to be caught and generally shouldn't be.

So, to evaluate the exceptions that you listed in your post, simply look them up in the api and see which types they descend from.

Hope that helps,
Joshua Smith


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
Thank you very much.

Your explains are very clear.
William CJ
Ranch Hand

Joined: Feb 28, 2006
Posts: 34
All Exception(s) extending java.lang.RuntimeException OR java.lang.Error are unchecked Exception. Every other Exception are checked.
Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
Thank you very much.

Your explains are very clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception question?
 
Similar Threads
Runtime & Compile time Error
Handling IOException in Data Accessing
How to recognise a checked or unchecked exception
Compiler :
funda behind exception...handling