• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

What makes an exception a checked or an Unchecked Exception

 
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?
 
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
 
Bartender
Posts: 1952
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: 1952
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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!