I'm confused by checked exception of java
Checked Exception requires to be handled at compile time using try, catch and finally keywords or else compiler will flag error
Read more: http://javarevisited.blogspot.com/2013/06/10-java-exception-and-error-interview-questions-answers-programming.html#ixzz3pk6OBSrj
My problem is: we all know "NoSuchMethodExcepion" is checked exception, and take the statement above into consideration, does it means that whenever I try to call a method, I should use try,catch to include the method calling code like this
try{
callingMethod();
}
catch(Exception){
}
But in fact, I don't need to do that right? Then what's the really meaning of the statement that given in the first place? Thanks you for answering my question.
NoSuchMethodException
is a subtype of ReflectiveOperationException
, so it can only be thrown by code that uses reflection. For normal method calls like your example, it is a compile-time error for the method to not exist, so your code won't compile at all.
If you compile your class with one version of a dependency and then run it with a different version, it is possible for a method that existed at compile time to disappear. In this case you'll get a NoSuchMethodError
(which is an Error
, not an Exception
) when the code is executed instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With