- 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?
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.
SCJP SCWCD AIX SOA AWS GA
The significant problems we face cannot be solved by the same level of thinking that created them -- Albert Einstein
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.
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.
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.
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.