Yes. You only have to look to the method signature. It doesn't matter what happens inside of the method (see it as a black box). If the signature declarers to throw a checked exception then the calling method must catch it, or declare to throw the same or a wider exception.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
yes, it IS following "handle or declare" rule. "callSomeMethod()" method in "MyException" class is calling a method "someMethod()" which declares that it throws an exception, so now "callSomeMethod()" must either handle it or declare it!
SCJP 5.0 100%
Chintan B Shah
posted 10 years ago
Thanks a lot guys...my doubt is now clarified.
Wouter, lightning struck my mind when I read "black box" scenario. So, thnx much dude.
The reason for posting out this question was due to my understanding that if you throw a new exception from a catch block and it is not declared in throws clause, the compiler complains...that's why I thought the compiler would be intelligent enough to see that if a called method(in this case someMethod()) abides by "Handle and Declare" rule then none of methods calling it should ever handle those exception as the exception will be caught and taken care of in called method(someMethod()) itself...does my argument makes any sense?
My doubt has been clarified, however, I still want to know opinion of experts out there on my argument.
Basically, if compiler is intelligent enough to throw compile time error on an exception(not declared in throws clause) thrown in catch clause .....why is it not intelligent enough to see that if a method is abiding by "handle and declare" rule for the exception, then all calling methods need not to catch that exception(unless its re-thrown)..and thereby not give any compile time error message.