Java : NoSuchMethodException with Examples

NoSuchMethodException (Java SE 19 & JDK 19) API Examples.
You will find code examples on most NoSuchMethodException methods.


Summary

Thrown when a particular method cannot be found.

Class diagram

public class Foo {
    public void aaa() {
    }
}
try {
    final var cls = Foo.class;

    final var aaa = cls.getMethod("aaa");
    System.out.println("aaa : OK!");

    final var bbb = cls.getMethod("bbb");

} catch (NoSuchMethodException e) {
    System.out.println(e);
}

// Result
// ↓
//aaa : OK!
//java.lang.NoSuchMethodException: Foo.bbb()

Constructors

NoSuchMethodException ()

Constructs a NoSuchMethodException without a detail message.

final var e = new NoSuchMethodException();
System.out.println(e); // java.lang.NoSuchMethodException

NoSuchMethodException (String s)

Constructs a NoSuchMethodException with a detail message.

final var e = new NoSuchMethodException("abcd");
System.out.println(e); // java.lang.NoSuchMethodException: abcd
System.out.println(e.getMessage()); // abcd

Methods declared in Throwable

addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

Please see the link below.


Related posts

To top of page