posted 21 years ago
I think a more appropriate statement would be
if a statement within a method throws a checked exception, that exception must be dealt within that method using a try/catch block or declared in the throws clause of the method
consider this simple code
aMethod() throws an exception at line 9, so it can either deal with this exception in the method body by using a try/catch block (which it doesn't!) OR propagate it to the caller by declaring it in its throws clause (which it does at line 8).
Next in the line is method main() (that btw too can declare Exceptions in its throws clause, just like any other method) that called aMethod() at line 4. Again, it can either deal with this probability of getting an exception by either using a try/catch block (which it does) OR propagate it to the caller (System in main's case) by declaring it in its throws clause.
Since aMethod() declares that it might throw an Exception, any method calling aMethod() must use a try/catch block or propagate it using throws clause. Failing to do either flags a compiler error. If aMethod() hadn't declared, then there is no responsibility on the calling body, but then aMethod() itself must deal with any exceptions it throws using a try/catch block.
so remember that you can't get past the compiler unless you "deal" with a checked exception by either using a try/catch block or propagating it using the throws clause.
Note that the rule DOES NOT apply to unchecked exception (Error, RuntimeException, & all subclasses of these two) are known as unchecked exception. All the rest are checked exceptions. A good program is not supposed to throw any unchecked exceptions in the first place, so a compiler gives no error if you throw a unchecked exception & don't "deal" with it in the above described manner.
I suggest you experiment with the above code. e.g. remove the "throws Exception" from aMethod(), compile & see what happens .. or comment try/catch block around aMethod(), compile & see what happens. Also try replacing Exception() at line 9 with RuntimeException() or Error() to get a better understanding of unchecked exception.
Hope this helps.