如何在 Java 中的自定义异常中设置我自己的消息,可以检索我的 getMessage() 但不使用构造函数,有什么办法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26313084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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?
提问by Sainath S.R
I'm just learning about exception handling in Java. What I would like to know is rather than trying something like say:
我只是在学习 Java 中的异常处理。我想知道的是,而不是尝试这样说:
throw new Exception("My Message");
and
和
String message=ex.getMessage();
System.out.println(message);
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 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
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()
虽然这工作正常,但我想知道我是否可以避免调用 super(message) 等,并在我的子类 MyException 中设置一些变量“message”,以更改调用 exception.getMessage() 时检索到的消息
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
换句话说,存储传递给构造函数的消息字符串的字符串变量的名称是什么,我可以手动设置它,它是最终的还是私有的,如果是的话,是否有任何设置方法。抱歉,我试过了,但我只是一个初学者,无法浏览 API
采纳答案by SamTebbs33
No, there is no way of setting the message manually, you could however just use your own variable instead and override the getMessage()method
不,无法手动设置消息,但是您可以使用自己的变量并覆盖getMessage()方法
Example:
例子:
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());
}
}
回答by iColdBeZero
String Variable that store the message string passed to the constructor and can i set it manually
存储传递给构造函数的消息字符串的字符串变量,我可以手动设置它
There is no such field in Java.
Java 中没有这样的字段。
But to help you, i suggest using ArrayList
to store Exceptions Strings. You can easily manipulate data with this by using add()
,remove()
, indexOf()
methods . All additional information about class you can find here.
但是为了帮助您,我建议使用ArrayList
来存储异常字符串。通过使用add()
, remove()
,indexOf()
方法,您可以轻松地使用它来操作数据。您可以在此处找到有关课程的所有其他信息。
Keep in mind that this is not only way to store sets of data, so i suggest to read documentation.
请记住,这不仅是存储数据集的方式,所以我建议阅读文档。