Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how overwrite Exception.getMessage

I have problem in exception.getMessage() method when raised for NullPointerException which returns null.
How can I overwrite base Exception.getMessage() to control it without changing in JRE?

like image 232
Moh Tarvirdi Avatar asked Sep 05 '26 18:09

Moh Tarvirdi


1 Answers

Try :

public class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }

    public MyException(Throwable throwable) {
        super(throwable);
    }

    public MyException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

Throw MyException one program.

try {

} catch (NullPointerException e) {
    throw new MyException("My exception message", e);
    or
    throw new MyException("My exception message");
    or
    throw new MyException(e);
}

Catch MyException another program.

try {

} catch (MyException e) {
    System.out.println(e.getMessage());
}
like image 116
Zaw Than oo Avatar answered Sep 07 '26 07:09

Zaw Than oo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!