英文:
Throw defined messages for a Custom Exception in Java
问题
我是Java编程的新手(实际上已经在学习了),关于如何处理不同的消息我有一些疑问。
我的目标是在同一个类(CustomException类)中包含这些不同的消息,以避免在其他类中抛出新的CustomExceptions的每个方法中一遍又一遍地编写相同的字符串。
到目前为止,我编写了以下代码:
-
一个继承自Exception的CustomException类,其中包含不同的消息(在示例中仅为两个,但实际上有更多),当然还有构造函数:
public class CustomException extends Exception { private static final String MSG_ERR_NUM1 = "错误编号1"; private static final String MSG_ERR_NUM2 = "错误编号2"; public CustomException() { super(); } public CustomException(String message) { super(message); } public CustomException(String message, Throwable cause) { super(message, cause); } public CustomException(Throwable cause) { super(cause); } }
-
其他不同的类中有一些方法,这些方法会抛出新的CustomException:
public class Car { public void checkWheels() throws CustomException { if (comparison = true) { throw new CustomException(如何调用??); } } }
-
一个带有主方法的类,该方法执行
try
和catch
:public class Driving { public static void main() { try { //做一些事情 } catch (CustomException e) { e.printStackTrace(); } } }
我的问题是:
-
是否应该在CustomException类上添加代码以获得从每个String中获取文本的不同getter方法?
public String getErrorNum1() { return MSG_ERR_NUM1; }
-
如何从其他类的方法的throws中调用这些getter方法?
if (comparison = true) { throw new CustomException(在这里如何获取MSG_ERR_NUM1或MSG_ERR_NUM2文本,而不是每次都编写一个String??); }
提前感谢您的支持。
英文:
I'm a newbie programming in Java (actually already learning) and I have some doubts about how to handle different messages.
My goal is to include in the same class (CustomException class) those different messages to avoid writing the same strings again and again on each method that throw new CustomExceptions from other classes.
Until now I coded:
-
A CustomException class that extends from Exception and has the different messages (only two in the example but there are many more) included as a Strings, and of course the constructors:
public class CustomException class extends Exception { private static final String MSG_ERR_NUM1 = "Error number 1"; private static final String MSG_ERR_NUM2 = "Error number 2"; public CustomException() { super(); } public CustomException(String message) { super(message); } public CustomException(String message, Throwable cause) { super(message, cause); } public CustomException(Throwable cause) { super(cause); } }
-
Other different classes with some methods on them that throw new CustomException:
public class Car { public void checkWheels() throws CustomException { if (comparison = true) { throw new CustomException(what to call??); } } }
-
A class with the main method where executes the
try
andcatch
:public class Driving { public static void main() { try { //doSomething } catch (CustomException e) { e.printStackTrace(); } } }
My questions are:
-
Would it be a good procedure to add code for different getter methods on CustomException class to get the text from each String?
public String getErrorNum1() { return MSG_ERR_NUM1; }
-
How would I call these getters from the throws included in the methods of other classes?
if (comparison = true) { throw new CustomException(what here to get MSG_ERR_NUM1 or MSG_ERR_NUM2 text instead writing a String every time??); }
Thanks in advance for your support.
答案1
得分: 1
你可以将所需的字符串添加到异常构造函数中的“message”中,如下所示:
public class CustomException extends Exception {
private static final String MSG_ERR_NUM1 = "错误编号 1";
private static final String MSG_ERR_NUM2 = "错误编号 2";
public CustomException() { super(); }
public CustomException(String message) { super(MSG_ERR_NUM1 + message); }
public CustomException(String message, Throwable cause) { super(MSG_ERR_NUM1 + message, cause); }
public CustomException(Throwable cause) { super(cause); }
}
如果您想为不同的情况抛出不同的消息,可以使用您的public static final String
字段:
当您抛出异常时,您可以提供枚举原因 throw new CustomException(CustomException.MSG_ERR_NUM2);
但我认为为不同的错误创建不同的异常类会更好和更清晰。
英文:
You can add the desired string to the "message" in the exception's constructor like:
public class CustomException class extends Exception {
private static final String MSG_ERR_NUM1 = "Error number 1";
private static final String MSG_ERR_NUM2 = "Error number 2";
public CustomException() { super(); }
public CustomException(String message) { super(MSG_ERR_NUM1 + message); }
public CustomException(String message, Throwable cause) { super(MSG_ERR_NUM1 + message, cause); }
public CustomException(Throwable cause) { super(cause); }
}
If you want to throw different message for different situations, you use your public static final String
fields:
and when you throw the Exception you give the enum cause throw new CustomException(CustomException.MSG_ERR_NUM2);
But I think it is better and cleaner to create different Exception class for different errors.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论