File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about 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 "a question about exception" Watch "a question about exception" New topic
Author

a question about exception

michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
public class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
}
}
static void method() {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
}
static void wrench() {
throw new NullPointerException();
}
}
the code above does compile.What confuse me is
static void wrench() {
throw new NullPointerException();
}
I think it should be
static void wrench() throws NullPointerException {
throw new NullPointerException();
}
[ March 06, 2002: Message edited by: michael wang ]

--<br />a java beginner from China
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
The name of your class, Unchecked, explains your situation.
In java, there is a special class of Exceptions, called unchecked exceptions. They all descend from/extend RuntimeException. RuntimeExceptions do not have to be declared like other types of exceptions. You can freely throw a RuntimeException without declaring that you do so. That's why you don't need to include the "throws NullPointerException" clause. It is of course perfectly legal to include it if you want to; but it's not mandatory, since it's an unchecked exception.


Rob
SCJP 1.4
michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
very clear,thanks
it's really very important to read it again and again.
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Thought I'd complete Rob's message.
Though this does not directly impact your question, but still for clarity's sake...
UnChecked exceptions comprise of the RuntimeException & its subclasses AND
Error & its subclasses.
So, even if you throw an exception which is of type Error or one of its subclasses, you wouldn't have to declare it as being thrown.
- Himanshu


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
To banter on, I'd like to make relate this to something you're probably even more familiar with, arrays. When you use the [] operator on an array to get a value from the array, this operator has the ability of throwing an ArrayIndexOutOfBoundsException. Yet, you don't have to catch it. If you did, every time you wanted to get something out of an array, you'd have to do this:

If you take a look at ArrayIndexOutOfBoundsException in the API, you'll see that it descends from RuntimeException, meaning that it is unchecked. Therefore, the try block I showed above, although perfectly legal, is optional.
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about exception