• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What makes an exception a checked or an Unchecked Exception

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic