Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Exception question?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

Your explains are very clear.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

Your explains are very clear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic