This content has been marked as final. Show 4 replies
But the predefined java class RunTimeException is also a subclass of Exception what makes it an unchecked exception.The fact that it extends RuntimeException.
Does compiler treat this class differently.Yes.
How I can make unchecked exception class when I extend my custom exception class from Exception.Make it extend RuntimeException. If you're sure that's what you want to do. In my opinion there are already enough exceptions that do that, if you need one, and application exceptions should be checked.
But the RunTimeException class is
Class RunTimeException extends Exception
As per this RunTimeException should also be checked exception, When you say compiler treats it differently, isn't it violating the general rule "if you extend custom exception class with Exception then it will be a checked exception".
I can extend it from RunTimeException but I just wanted to know the valid reason behind it. I expect compiler to treat all the class equally.
isn't it violating the general rule "if you extend custom exception class with Exception then it will be a checked exception"?No, because there is no such rule. You made it up.
The rule is that an exception class that extends Exception is a checked exception unless it extends RuntimeException.
Is this really so mysterious?
But the predefined java class RunTimeException is also a subclass of Exception what makes it an unchecked exception. Does compiler treat this class differently.
How I can make unchecked exception class when I extend my custom exception class from Exception.
The answer to both of these questions can be found in Chapter 11.2 'Compile-Time Checking of Exceptions of the Java Language Specification. Suggest you read the entire section.
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.
These two other subsections give the reasons the designers of Java made the choices above
11.2.4 Why Errors are Not Checked
11.2.5 Why Runtime Exceptions are Not Checked