构造函数中多个参数的异常
我想知道在一个构造函数中创建具有多个参数的异常是否可以(与 throwable、字符串不同),或者这种做法是否不好?
为什么我需要一个带有多个参数的异常,好吧,假设我正在分析一个矩阵,当出现错误时,我会引发一个带有位置的异常。我想在异常中给出明确的错误消息,并且我想使用国际化,这意味着不同语言的消息。
例如,消息可以是:
位置4、5有错误。
Hubo un Problema en la fila 4 con columna 5.
如您所见,两条消息的文本不同,并且为了具有描述性,这些值对于消息很重要。
我的方法是这样的:(
public class MatrixException extends Exception {
int x;
int y;
public MatrixException (int x, int y){
this.x = x;
this.y = y;
}
public String getMessage(){
return Messages.getString("MatrixException.Message1") + this.x
Messages.getString("MatrixException.Message2") + this.y
Messages.getString("MatrixException.Message3");
}
}
Messages
类实现了 ResourceBundle
类)
对于这种异常,我可以用相应的语言创建描述性消息,但是我有从未见过参数与 String 和 Throwable 不同的异常。
我试图找到有关如何编写定义良好的异常层次结构的信息,但没有很多文档,也没有关于构造函数的信息。
您认为我的方法怎么样?
I would like to know if it is fine to create an exception with multiple parameters in one constructor (different to throwable, string) or if this practice is bad?
Why do I need an exception with multiple parameters, well, let's suppose I am analyzing a matrix, and when there is an error, I will raise an exception with the position. I would like to give a clear error message in the exception, and also I would like to use internationalization, that means, messages in different languages.
For example, the messages could be:
There is an error in the position 4, 5.
Hubo un problema en la fila 4 con columna 5.
As you can see, the text is different for both messages, and the values are important for the message in order to be descriptive.
My approach is something like this:
public class MatrixException extends Exception {
int x;
int y;
public MatrixException (int x, int y){
this.x = x;
this.y = y;
}
public String getMessage(){
return Messages.getString("MatrixException.Message1") + this.x
Messages.getString("MatrixException.Message2") + this.y
Messages.getString("MatrixException.Message3");
}
}
(The Messages
class implements the ResourceBundle
class)
With this kind of exception, I could create a descriptive message in the corresponding language, however I have never seen Exceptions with parameters different to String and Throwable.
I have tried to find information about how to write a well-defined exception hierarchy, but there is not a lot of documentation, and nothing about the constructors.
What do you think about my approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这种方法没有什么问题。
事实上,标准库中有一些异常类,它们的构造函数采用与
String
和Throwable
不同的参数。我想到的第一个例子是
SQLException(String, String, int)
。然后是URISyntaxException(String, String, int)
甚至EnumConstantNotPresentException(Class, String)
。There's nothing wrong with this approach.
In fact, there are a few exception classes in the standard library with constructors that take arguments different to
String
andThrowable
.The first example that comes to mind is
SQLException(String, String, int)
. Then there'sURISyntaxException(String, String, int)
and evenEnumConstantNotPresentException(Class<? extends Enum>, String)
.我喜欢它。您的另一个选择是格式化字符串并使用正常的 Exception 构造函数,但我认为您所拥有的很好。
I like it. Your other option would be to format the string and use the normal
Exception
constructor, but I think what you have is fine.当然。如果自定义异常类中有其他参数/字段,则在抛出异常时可以更轻松地评估问题,请添加。
Sure. If have additional parameters / fields in your custom exception class will make it easier to assess an issue if the exception is thrown, add away.
你可以重写方法 toString 而不是方法 getmessage。
U can override method toString instead of method getmessage.