如何使用Java中的构造函数中的数字创建自定义异常 [英] how to create custom exception with a number in constructor in Java
本文介绍了如何使用Java中的构造函数中的数字创建自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我使用String消息创建自定义异常,我有: super(message)
但是,我想在我的异常消息中有一个双重数字。
构造函数将像
myException:我的消息:90.6
。抛出异常:抛出新的myException(我的消息,90.6);
If I create custom exception with a String message i have: super(message)
however, i want to have a double number in my exception message.
The constructor will be something like
myException: my message:90.6
. throw exception: throw new myException("my message", 90.6);
这是我尝试但没有工作的代码。任何帮助将会非常欣赏。
Here is the code i've tried but didnt works. Any help would be much appreciate.
public class myException extends Exception {
private String message;
private double qtyAvail;
public myException(String message, double qtyAvail){
super(message);
this.qtyAvail = qtyAvail;
}
推荐答案
不要你做
public MyException(String message, double qtyAvail){
super(message + " " + qtyAvail);
}
这篇关于如何使用Java中的构造函数中的数字创建自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文