How can I set my own message in my custom exception in java that can be re-handled by my getMessage () BUT WITHOUT Using a constructor, is there any way?

I am just learning about exception handling in Java,

What I would like to know, rather than try something like say,

throw new Exception("My Message");



String message=ex.getMessage();



Take a look at the code below,

class ExceptionTest {
    public static void main(String[] args) {
        ExceptionTest t1=new ExceptionTest();

        try {
            t1.riskyMethod();//call the risky or exception throwing method
        } catch(MyException myex) {
            System.out.println("Exception has been thrown");

            String message=myex.getMessage();//get the String passed during exception call
            System.out.println("The message retrived is "+message);
            myex.printStackTrace();//prints name of exception and traces the function call stack

    }//main ends

    void riskyMethod() throws MyException {//a method that can throw an excpetion
        int rand=(int)(Math.random()*10);///Math.rand return 0 to .9 range value

        if(rand>5) {
            //throw new MyException();  or try this
            //      MyException myexception=new MyException();
            //      myexception.setMessage("HI THIS IS A SAMPLE MESSAGE");
            String mymessage="Sample Exception Message...";
            throw new MyException(mymessage);
            System.out.println("No exception");
}//Exception class ends 


While this works great, I want to know if I can avoid calling super (message), etc. and just set some kind of variable "message" in my subclass of MyException that modifies the message received when exception.getMessage () is called

In other words, what is the name of the String variable that stores the message string passed to the constructor and can I set it manually, whether it is final or private, if there is any setter method for that, sorry but I tried, but I just getting started and having difficulty navigating the API


source to share

2 answers

No, there is no way to customize the message manually, however you can just use your own variable and override the getMessage () method


public class MyException extends Exception{

    public String message;

    public MyException(String message){
        this.message = message;

    // Overrides Exception getMessage()
    public String getMessage(){
        return message;

    // Testing
    public static void main(String[] args){
        MyException e = new MyException("some message");




String variable that stores the message string passed to the constructor and can I set it manually

There is no such field in Java.

But to help you, I suggest using ArrayList

to store exception strings. You can easily manipulate the data using the methods add()

, remove()

, indexOf()

. All additional information about the class can be found here .

Keep in mind that this is not only a way to store datasets, so I suggest reading the documentation.



All Articles