Difference between NoSuchMethodException and NoSuchMethodError in Java
Asked Answered
A

4

16

I can't find exact difference between NoSuchMethodException and NoSuchMethodError in Java. Could someone give explanation and example of these two things ?

Abbacy answered 14/1, 2015 at 8:42 Comment(1)
NoSuchMethodException occurs when you call a method where you might expect the target method might be missing. NoSuchMethodError occurs when the method is missing but normally it wouldn't be i.e. in normal code.Caliber
G
27

NoSuchMethodException can be thrown when you're invoking a method through reflection, and the name of the method comes from a variable in your program.

NoSuchMethodError can be thrown when a compiled Java class does a regular method call to another class and the method doesn't exist. (This usually happens when the caller class was compiled against one version of the class being called, and is being executed together with another version of that class, which no longer has the method.)

Gauhati answered 14/1, 2015 at 8:46 Comment(3)
About the NoSuchMethodException case, does the name of the method actually need to come from a variable for this exception to be thrown ?Hoxsie
Well, it could be a field, or the result of calling a method or evaluating an expression.Gauhati
Sure, actually my question wasn't too clear, I meant can it be a string literal or constant ?Hoxsie
W
15

NoSuchMethodException occurs when you try to invoke a method using reflection. NoSuchMethodError occurs when you had that method while compiling but dont have it during the runtime.

Consider the following example for NoSuchMethodError

Class : Person.java

public class Person{
      public String getName(){
         return "MyName";
     }
}

Compile it using javac Person.java And now try to run this using java Person

It ll give you

java.lang.NoSuchMethodError: main
Exception in thread "main"

Because it tries to find the public static void main(String [] args) which is not there

For NoSuchMethodException

c = Class.forName("java.lang.String");
  try
  {
    Class[] paramTypes = new Class[2];
    Method m = c.getDeclaredMethod("myMethod", paramTypes);
  }

this is ll throw an exception saying

java.lang.NoSuchMethodException: java.lang.String.myMethod(null, null)

Consider this link which has a better explaination

Wanderoo answered 14/1, 2015 at 8:47 Comment(0)
S
7

NoSuchMethodException is thrown when you try and get a method that doesn't exist with reflection. E.g. by calling Class#getDeclaredMethod(name, parameters) with either the wrong name or parameters.

NoSuchMethodError is thrown when the virtual machine cannot find the method you are trying to call. This can happen when you compile with one version of a library, and later run the application with another version of the library on the classpath (e.g. an older one that doesn't have the method you're calling)

Slangy answered 14/1, 2015 at 8:55 Comment(0)
T
2

Class NoSuchMethodException:

Thrown when a particular method cannot be found.

Class NoSuchMethodError

Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

Also see this article, it explains it better.

Tameratamerlane answered 14/1, 2015 at 8:45 Comment(6)
Hai Maroun : I can't understand "class no longer has a definition of that method" Please explain this statement.Abbacy
This means that the calling class is not aware of the changed class. The method was there when the calling class was compiled but while the runtime - something changed and its not able to find it anymoreWanderoo
@Abbacy You have a method A.method1() which calls B.method2(), you remove B.method2() without recompiling class A. When A.method1() is run you get a NoSuchMethodErrorCaliber
I caught one more exact answer from Peter Lawrey.Thank's Peter.Abbacy
The linked page says Since [NoSuchMethodException] is a Exception you can simply catch it and can proceed with your flow, where as it is not possible with NoSuchMethodError Any idea why it says it's not possible ? It might not be advisable to catch Errors, or something else as subjective as that, but it's definitely possible, and it should be possible for any Throwable.Hoxsie
@Hoxsie The Error class and subclasses, including NoSuchMethodError, have no good way to handle other than logging/reporting at the top level. Errors are unexpected conditions that the programmer is not expected to handle. Also globally catching Error or Throwable fucks with certain functionality (namely threads stopping each other -- the stop works via an Error being injected into the target thread)Divalent

© 2022 - 2024 — McMap. All rights reserved.