File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unchecked Exception

 
Brian Smith
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I tried to search explanations on Unchecked Exception (sub class of Runtime Exceptions) but could not find the one that made my understanding clear. I would apprecieate if someone could explain me WHY runtime exceptions are unchecked. All the time I get definitions that Runtime Excpeptions are not checked by compiler while compiling the codes. BUT WHY?

Also, how does an Error differ from an Exception?

Thanks.
 
Steve McCann
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
As the name implies, a RuntimeException is one which occurs at run time. The compiler cannot check for these as variables have not been assigned values yet - you may be dividing by zero for example.

You can handle them if you want, if you divide by zero it might be helpful to have an elegant way of dealing with it.

An Error is a subclass of Throwable (so is Exception) and is used when the JVM has a serious error which should not be caught.

Steve
 
Nicky Eng
Ranch Hand
Posts: 378
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve is right. The error and Exception are the subclasses of Throwable.

if you have the K&B book, you can go to page 245 (figure 4-2) hierarchy diagram.

RuntimeException is unchecked exception, so is Error.

All other exceptions(under Exception class), meaning all those that do not derive from java.lang.RuntimeException, are checked exceptions. A checked exception MUST be caught somewhere in your code. that means that unchecked exceptions you dont have to caught it in your code,but if you want to also can.

correct me if i'm wrong....thanks in advance.
[ June 28, 2005: Message edited by: Nicky Eng ]
 
Manuel Moons
Ranch Hand
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A RuntimeException is not an Error.

There are 3 types of exceptions:

Checked exceptions --> these must be caught or compilation will fail.
Unchecked exception --> these can be caught but the compiler will not complain if they are not caught.
Errors --> These can not be caught because these exceptions will indicate that there is a serieus problem eg. OutOfMemoryError indicates that there is no more memory available and so normal flow cannot continue.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic