O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Throwing Exceptions

Your method might not be the very best place to handle an exception. It might make more sense to let the exception pass through your method unhandled, and make some other guy deal with it. You can do that. But you have to say that that is what you're doing. You do so with the throws keyword. The following modified method will also compile and run:

public void someMethod () throws IOException {
       File f = new File("s");
       f.getCanonicalFile();
}

Do not declare that your method throws unchecked exceptions that inherit from Runtime-Exception. Do not write the following:

void do(int i) throws NullPointerException
     //BAD DOG! NO!
{
     //blah blah blah
}

Why should you never do that? Sometimes the truth is tough. If you are that worried ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required