• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

what is the difference between checked and unchecked exceptions

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between checked and unchecked exceptions?
How is it related to runtime exception
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
manesh,
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.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
Object
|_ 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.
cheers,
Kathy
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


ThrowableTest.java:11: unreported exception MyThrowable; must be caught or declared to be thrown
void f() {throw new MyThrowable();}
^
ThrowableTest.java:13: 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.
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!

 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic