If a method might throw a checked exception, it must be declared in the signature of the method (as throws ). Unchecked exceptions do not have to be listed in the method header.
If code calls a method that may throw a checked exception, the calling code must handle the exception. It can handle the exception by either catching it or by declaring in its own signature that it may throw that exception type. Unchecked exceptions do not have to be handled by calling code.
In the K&B book it is said that a checked exception that is rethrown in the catch block has to be both handled and declared, but I think this is wrong, handling it will do the job. But that will lead to some ugly nesting:
compiles without warning and prints: main ready
all events occur in real time
Joined: Dec 07, 2004
Originally posted by wise owen: [QB]If a method might throw a checked exception, it must be declared in the signature of the method (as throws ).
But if you handle it(using try and catch)in the same method, you don't need to declare it. That means declaration is only OPTIONAL, NOT A MUST; am I right?