aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
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

Joined: May 10, 2001
Posts: 3244
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


Dave
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5264
    
    9

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
David Eason
Greenhorn

Joined: Oct 31, 2001
Posts: 14
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).]
 
wood burning stoves
 
subject: Exceptions