Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checked Exception requires to be handled at compile time using try, catch and finally keywords or else compiler will flag error

Tags:

java

exception

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.

like image 382
lanx86 Avatar asked Oct 27 '15 05:10

lanx86


1 Answers

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.

like image 100
Daniel Pryden Avatar answered Oct 08 '22 20:10

Daniel Pryden