• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question?

 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

Your explains are very clear.
 
William CJ
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Exception(s) extending java.lang.RuntimeException OR java.lang.Error are unchecked Exception. Every other Exception are checked.
 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

Your explains are very clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic