如何在我的自定义异常中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗? [英] How to set my own message in my custom exception in Java that can be retrieved my getMessage() BUT WITHOUT using the constructor, is there any way?

查看:199
本文介绍了如何在我的自定义异常中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学习Java中的异常处理。我想知道的不是尝试说:

I'm just learning about exception handling in Java. What I would like to know is rather than trying something like say:

throw new Exception("My Message");

String message=ex.getMessage();

System.out.println(message);

看看下面的代码,

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 retrieved 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);
        }
        else
            System.out.println("No exception");
    }
}//Exception class ends 

虽然这样工作正常我想要要知道我是否可以避免调用super(message)etc
并在我的子类MyException中设置一些变量'message',它会更改在调用exception.getMessage()时检索到的消息

While this works fine I want to know if I can avoid calling super(message) etc and just set some variable 'message' in my subclass MyException that changes the message retrieved on a call to exception.getMessage()

换句话说,存储传递给构造函数的消息字符串的字符串变量的名称是什么,我可以手动设置它,是最终的还是私有的,如果有的话,是否有任何setter方法。抱歉,我试过,但我只是一个初学者,无法导航API

In other words what is the name of the string variable that store the message string passed to the constructor and can I set it manually, is it final or private, if so is there any setter method for it. Sorry I tried but am just a beginner and have trouble navigating the API

推荐答案

不,没有办法手动设置消息,你可以只改用你自己的变量并覆盖 getMessage()方法

No, there is no way of setting the message manually, you could however just use your own variable instead and override the getMessage() method

示例:

public class MyException extends Exception{

    public String message;

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

    // Overrides Exception's getMessage()
    @Override
    public String getMessage(){
        return message;
    }

    // Testing
    public static void main(String[] args){
        MyException e = new MyException("some message");
        System.out.println(e.getMessage());
    }


}

这篇关于如何在我的自定义异常中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆