This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between checked and unchecked exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference between checked and unchecked exceptions" Watch "what is the difference between checked and unchecked exceptions" New topic

what is the difference between checked and unchecked exceptions


Joined: Oct 07, 2002
Posts: 1
what is the difference between checked and unchecked exceptions?
How is it related to runtime exception

Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch
Unchecked exceptions and runtime exceptions are one and the same.
Checked exceptions are exceptions your program needs to catch and handle to compile successfully. Runtime (unchecked) exceptions do not need to be caught.
Please refer to JLS 11.2 Compile-Time Checking of Exceptions for details about checked and unchecked exceptions.
Moreover, we'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

[Blog] [Blogroll] [My Reviews] My Linked In
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
Hey Manesh,
Exceptions can be two types checked and unchecked. Unchecked exception include errors and RunTimeExceptions. Runtime exceptions happen mainly because of bad programming practices that shouldn't have happened. For example Null pointer exception is one example, if a programmer makes sure that a value is not null this wont be thrown. Its called unchecked since the programmer doesn't have to deal with it explicitly, i.e he doesnt have to catch them or declare that that they will be thrown during the operation coz they shouldn't happen in the first place.
Secondly checked exceptions are the ones that can happen to an otherwise well programmed code. So in this case the programmer tells the compiler that what can go wrong. For example if a code is trying to read from a file, it must give in to the possibility that what if the there is some IO problem therefore tell the compiler catch or throw IOException if encountered.
Hope this helps u out!

Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Just to add one more thing...
The way the compiler knows which are checked and which are unchecked, is that checked exceptions must be subclasses of Exception, but must NOT be of type RuntimeException (in other words, it can't be a subclass of RuntimeException).
|_ Throwable
|_Error <-- not checked
|_Exception <-- checked
|RuntimeException <-- not checked
But you also need to know that there are other things that are "Throwable" (which means they can be "caught") even though not checked by the compiler. Anything that subclasses Throwable (so, Error, Exception, RuntimeException and all their subtypes) can be caught, declared, thrown, whatever -- but only the ones that subclass Exception but do NOT subclass RuntimeException are the ones that must be "handled or declared".
Be sure you know that AssertionErrors are Errors and not Exceptions, but that means they are still Throwables, and thus can be caught... even though you should NEVER catch them. You're expected to clearly understand all these implications.
And don't forget that you can always create your own exceptions -- both checked and unchecked -- even though you're discouraged from making your own UNchecked exceptions. Still legal, though.

Co-Author of <a href="" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I hesitate to argue with Kathy Sierra, who helped write the SCJP 1.4 exam.
Nevertheless, my (1.3) compiler considers Throwable itself, and any home-brewed subclasses of it, to be checked:

The compiler complains: unreported exception MyThrowable; must be caught or declared to be thrown
void f() {throw new MyThrowable();}
^ unreported exception java.lang.Throwable; must be caught or declared to be thrown
void g() {throw new Throwable();}
2 errors

It looks to me like all Throwables are checked, except for Error and its subclasses, and RuntimeException and its subclasses.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Ron Newman:
[QB]I hesitate to argue with Kathy Sierra, who helped write the SCJP 1.4 exam.
Hesitate is the LAST thing you should do ; )
Thankfully, there are many expert reviewers on the exam questions before they finally go "live". I'm just as prone to mistakes as the next cowgirl.
And you make a good point about Throwable, but since there *are* no known subclasses of Throwable in the API except for Exception and Error, that's why we think of it that way. So I should have said "throwable things" as a concept, rather than Throwable the class itself. So yes, if one were to actually subclass Throwable they'd have to answer to the compiler, but you're not supposed to -- the intention is that ALL throwable things at this point will be either Error or Exception (including RuntimeException). Still, Throwable has to be considered checked for polymorphic purposes (i.e. because it MIGHT be referring to an Exception rather than Error or RuntimeException).
But since there aren't any Throwables but Error and Exception (and their subs), we really don't *normally* consider it as a type of its own. But then again, while I'd love to say, "Oh, the exam would never get into something that you would never ever do, you all know that's not true ; )
So, excellent point!
I agree. Here's the link:
subject: what is the difference between checked and unchecked exceptions
Similar Threads
Checked & Unchecked Exceptions
Checked vs Runtime Exceptions
What is difference between Checked and Uncheched exceptions ?