aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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)


May the force of the Java be in all of us !!!
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
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

Joined: Sep 13, 2002
Posts: 331
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:


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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

Joined: Oct 26, 2000
Posts: 188
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


My theory of evolution is that Darwin was adopted. - Steven Wright
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
Is there also a heirarchy tree for checked and unchecked exceptions
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exceptions