Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between a runtime exception and a checked exception, and how do you know what kind of exception is what type. Ex: is an ArithmeticException a runtime or checked exception?
Also, why does the roundup game say that subclass methods that override superclass methods cannot declare new Runtime exeptions but can for checked exceptions? (or is it can declare new checked exceptions but not runtime)
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CheckedException-> The exceptions which can be handled by the programmer. In some scenarios, programmer can anticipate the type of exceptions that can be thrown from the application. As for example, whenever we do file operation, we know that it might throw FileNotFoundException if it can't locate the particular file.
UncheckedException-> The exceptions which can't be handled by the programmer, those are Unchecked exceptions. All unchecked exceptions are subclass of RuntimeException. These exceptions are ususally hadled by JVM. Programmer don't have control over these exceptions, so JVM takes care of that.
--------------
Nayan.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nayanjyoti Talukdar:
CheckedException-> The exceptions which can be handled by the programmer. In some scenarios, programmer can anticipate the type of exceptions that can be thrown from the application. As for example, whenever we do file operation, we know that it might throw FileNotFoundException if it can't locate the particular file.
UncheckedException-> The exceptions which can't be handled by the programmer, those are Unchecked exceptions. All unchecked exceptions are subclass of RuntimeException. These exceptions are ususally hadled by JVM. Programmer don't have control over these exceptions, so JVM takes care of that.

Sorry Nayan, but I don't like your definition.
A checked exception means that the [compiler enforces that the code must be placed in a try/catch block or the method must declare that it throws the exception.
An unchecked exception is a RuntimeException or any subclass of RuntimeException and the compiler does not force the programmer to explicitly handle them. However, the programmer can still definitely handle an unchecked exception. For example, ArithmeticException is an unchecked exception because it extends RuntimeException, so you can get away with this:

But to handle that unchecked exception (division by 0) you could do this:
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a tree of what exceptions are subclasss of what? Something like
Throwable
|______________
| |
IOException MalformedURLException
_|_____ _____|______
| | | |
.
.
.
.
I want to see the exception heirarchy tree so that if there is a question 'is MalformedURLException a checked or runtime exception' I know that it is so and so.
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the javadoc. They all have their hierarchy and their direct know subclasses, so if you check the javadoc for Exception and RuntimeException it will show you what you want to know.
Exception
RuntimeException
E
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there also a heirarchy tree for checked and unchecked exceptions
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That *is* the hierarchy for checked and unchecked exceptions. As stated earlier, unchecked exceptions are subclasses RuntimeException. Everything else is checked (at least as far as I know).
[ January 22, 2003: Message edited by: Layne Lund ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Nayanjyoti's explanation and Blake's explanation.
If you have some free time, to learn the basics of exception handling, I recommend reading
  • chapter 9 of Bruce Eckel's Thinking In Java Book
  • chapter 9 of David J. Eck's Introduction to Programming Using Java
  • chapters 80 & 81 of Bradley Kjell's Introduction to Computer Science using Java
  • Dick Baldwin's The Essence of OOP using Java, Exception Handling Article and
  • The Handling Errors with Exceptions Lesson of Sun's Java Tutorial
  • Then, when you're ready to have some fun, take a look at this past JavaRanch conversation as well as http://c2.com/cgi/wiki?IlluminateTheMainline
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic