• 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

custom exceptions

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

- When we extend the Exception class, those custom Exceptions are treated as Checked exception

- When we extend the RuntimeException class, those custom Exceptions are treated as UnChecked exception. My query is why? Since the parent class of RuntimeException is also Exception why custom classes extending RuntimeException are not treated as checked exception.

What is the code written in Exception class which stops this behaviour?

Regards,
Neeraj.

 
Ranch Hand
Posts: 81
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is managed by JVM. Java manages exceptions itself and i think it must have condition to check if it is runtime or not. Its like if you write your custom exception handler , each exception comes to you at first place and then you filter out the exceptions.
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a great big section in the Java™ Tutorials about exceptions, which covers this question. It is the inheritance hierarchy of the Exception class which determines whether it is checked or unchecked.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bacuase the JLS says so.
http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.

 
Saloon Keeper
Posts: 13889
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, all Throwables are checked, and I guess the compiler makes an explicit check whether an exception is a subtype of Error or RuntimeException to determine if the exception should be unchecked.

[edit]

Damn you Darryl!
 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
8 seconds between the two posts
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

thanks to all of you. I was also aware of these things. But was thinking from a different point of view. is this not violating basic oops principle of inheritence ? child is inherting from its parent but in some cases its not like its parent

consider this scenario

1.) Class A extends Thread
2.) Class B extends Thread
3.) Class C extends A


can you say that class C is not a Thread only A and B are Threads? I believe in case of exception its happening like above scenario. please. correct me if I am wrong.

Thanks again for your inputs.

Regards,
Neeraj.

 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also say that RuntimeException adds behaviour by being able to slip through the compiler undetected. That sounds like hair-splitting to me.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this good and correct ?

thnx
neeraj
 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's "thanks" please, not "thnx".

That is a good question. You will have to Google for Java checked exception failed experiment and see what you find. There has been a lot of discussion about checked Exceptions, and many people think it is a flawed concept. I don't f aeelble to join the controversy myself at the moment, but it will make for interesting reading. Please post a few of the more interesting links, so we can all have a look at them.
 
For my next trick, I'll need the help of a tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic