• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, all checked exceptions must be either caught (via Catch statement) and/or thrown (via throw and throws statement). Is this correct? Also, I read that throw throws the exception up 1 level on the call stack. Doesn't it just throw the exception right there?
In the Java 2 Platform SE 1.3.1 (javadoc), how can you determine from it that a listed exception for a method is a checked or unchecked exception, since both are a subclass of class Exception?
I did a lookup on the Thread.sleep method and it's listed Interrupted Exception was listed, however, it didn't say whether it was checked or not. Where in this javadoc can you determine this?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gary Farms:
As I understand it, all checked exceptions must be either caught (via Catch statement) and/or thrown (via throw and throws statement). Is this correct? Also, I read that throw throws the exception up 1 level on the call stack. Doesn't it just throw the exception right there?

If you call a method that states that it can throw an exception you ust either handle it in your code (in a try-catch block) or specify that your method that calls that method throws that exception. If your method throws an exception and it is not in a try-catch block it will throw it to the method that called your method and so on up the call stack. If it is in a try-catch block it will search your catch statements for a match, if it doesn't find one it will then throw it up the stack.

In the Java 2 Platform SE 1.3.1 (javadoc), how can you determine from it that a listed exception for a method is a checked or unchecked exception, since both are a subclass of class Exception?

All exceptions are checked except those that extend Error and those that extend RunTimeException. to find out if it is checked or not just check the JavaDoc for that Exception and look at it heirarchy so see what class it is derived from.
hope that helped

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything that is a RuntimeException or Error is unchecked. All other exception types are checked. In the JavaDocs, if you see RuntimeException or Error in the class hierarchy at the top, it's an unchecked exception.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are WRITING an exception and you have to decide whether is should be checked or unchecked this like this:
If the program could conceivably recover from the problem, then make it a checked exception - just extend Exception.
If it would not be reasonable to recover from the error then extend RuntimeException.
Only System Programmers (like those guys at Sun or folks writing JVMs) should try to extend Error.
------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
David Eason
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Venners has written an exceptional article that gave clear answers to every question I could think of about exceptions.
http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html
As for your exception of interest:

<PRE>
java.lang.Object
|
+--java.lang.Throwable
|
+--java.lang.Exception
|
+--java.lang.InterruptedException
</PRE>

As described in Figure 4 from the Java World article, all Exceptions are checked except for RuntimeException and its progeny. Hence java.lang.InterruptedException is a checked exception.

[This message has been edited by David Eason (edited November 06, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic