Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.Exception not a checked exception?

 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks!
 
Junilu Lacar
Bartender
Posts: 7589
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I just assumed that since I can catch an Exception without throwing it, then it isn't a checked exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic