This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What makes an exception a checked or an Unchecked Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What makes an exception a checked or an Unchecked Exception" Watch "What makes an exception a checked or an Unchecked Exception" New topic
Author

What makes an exception a checked or an Unchecked Exception

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
Hi

I saw the source files for RuntimeException and IOException classes
and they are both defined in the same way. Then what is that which makes
one a checked exception and another and unchecked exception?

Also can we create both unchecked and unchecked exceptions?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Rajiv Rai wrote:I saw the source files for RuntimeException and IOException classes
and they are both defined in the same way. Then what is that which makes
one a checked exception and another and unchecked exception?

A unchecked exception is any class that is a subclass (either directly or indirectly) of RuntimeException

Rajiv Rai wrote:Also can we create both unchecked and unchecked exceptions?

Yes
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1924
    
    7

RuntimeException and its subtypes are treated as unchecked exceptions, because that's the way the JLS defines it
JLS 11.1.1 wrote:
The class RuntimeException is a direct subclass of Exception. RuntimeException is the superclass of all the exceptions which may be thrown for many reasons during expression evaluation, but from which recovery may still be possible.
RuntimeException and all its subclasses are, collectively, the runtime exception classes.
...
The unchecked exception classes are the runtime exception classes and the error classes.
The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
So I guess it means that though both RuntimeException and IOException are
direct subclasses of Exception class .. its just that the compiler treats RuntimeException
as unchecked exception and IOException as checked exception to comply with the JLS

So the differentiation between them is at the compiler level
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1924
    
    7

Rajiv Rai wrote:So I guess it means that though both RuntimeException and IOException are
direct subclasses of Exception class .. its just that the compiler treats RuntimeException
as unchecked exception and IOException as checked exception to comply with the JLS

So the differentiation between them is at the compiler level


Right. Only the compiler cares about checked vs. unchecked exceptions.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: What makes an exception a checked or an Unchecked Exception
 
jQuery in Action, 3rd edition