Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What makes an exception a checked or an Unchecked Exception

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic