File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang.Exception not a checked exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java.lang.Exception not a checked exception?" Watch "java.lang.Exception not a checked exception?" New topic

java.lang.Exception not a checked exception?

Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
I find it odd that java.lang.Exception isn't a checked exception, but its subclasses are (except for java.lang.RuntimeException, of course.) Is there a reason why?
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

I think I have an inkling of how you came to this conclusion (I had the same doubt). But no, java.lang.Exception is in fact a checked exception. The JLS 11.2 states that "The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses."
If you have the following line
throw new Exception("yes it's checked");
in a method that doesn't handle Exception in a try/catch block or declare it in the throws clause then you get a compile error.
[ June 14, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Having read the post given by Junilu
the following cite...
From JLS 11.2

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor.

...makes me to think the following:
If no checked exception is thrown in a try, and no checked exception is caught; compiler doesn't enters the proccess of checking the reachability of catch clauses that trapped non-checked exceptions. On the other hand , maybe Jane is right. The fact that RuntimeException can theoretically be thrown anywhere, applies too within an empty try.
In a previous thread somebody said that the reason why an Exception, though a checked one, can be caught without being thrown is because is the parent of RuntimeException. Doesn't make sense that if a kind of exception can be caught without being thrown, its parents will exhibit the same behaviour? After all, the derived exceptions must be trapped,as well, by clauses catching base exceptions.

SCJP2. Please Indent your code using UBB Code
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Thanks all. I just assumed that since I can catch an Exception without throwing it, then it isn't a checked exception.
I agree. Here's the link:
subject: java.lang.Exception not a checked exception?
It's not a secret anymore!