Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示自定义throw语句_Java - Fatal编程技术网

Java 如何显示自定义throw语句

Java 如何显示自定义throw语句,java,Java,所以我有一个方法,就是在键盘上输入用户输入,然后插入计算器。显然,我不能将字符串或负数放入输入字段,所以我将所有三个输入都封装在一个try块中,以捕获任何异常。问题是,当try块内的输入错误时,我试图抛出异常,但当捕获到异常时,它会显示try-catch块的消息。代码如下: public static void calculateTips() { //Varibles int tipPercentage = 0; int partySize = 0; double billAmount = 0.

所以我有一个方法,就是在键盘上输入用户输入,然后插入计算器。显然,我不能将字符串或负数放入输入字段,所以我将所有三个输入都封装在一个try块中,以捕获任何异常。问题是,当try块内的输入错误时,我试图抛出异常,但当捕获到异常时,它会显示try-catch块的消息。代码如下:

public static void calculateTips()
{
//Varibles
int tipPercentage = 0;
int partySize = 0;
double billAmount = 0.0;
//Variable to loop through try catch blocks 
int x = 0;

String yesOrNo;

 boolean choice;
//Decimal Format 
DecimalFormat dollar = new DecimalFormat("$###,##0.00");

//Declare object
TipCalculator bill;

//Create Scanner object
Scanner userInput = new Scanner(System.in);

do
{
  //Fill values for tip calculator from user 
  System.out.println("Enter the bill amount: ");

  try
  {

     billAmount = userInput.nextDouble();

     if(billAmount < 0)

      throw new Exception("Bill amount can not be negative. Please try again."); 



     System.out.println("Enter your desired tip percentage (20 equals 20%): ");
     tipPercentage = userInput.nextInt();

     if(tipPercentage < 0)
       throw new Exception("Tip percentage can not be negative. Please try again."); 

     System.out.println("Enter the size of your party: ");
     partySize = userInput.nextInt();

      if(partySize < 0)
        throw new Exception("Party Size can not be negative. Please try again."); 


  }
  catch(Exception e)
  {
   System.out.println("Invalid Input. Please try again." + "\n");
   calculateTips();
  }
publicstaticvoidcalculatetips()
{
//各种
int-tipPercentage=0;
int partySize=0;
双倍金额=0.0;
//用于在try-catch块中循环的变量
int x=0;
字符串yesOrNo;
布尔选择;
//十进制格式
DecimalFormat dollar=新的DecimalFormat($######,##0.00”);
//声明对象
计算账单;
//创建扫描仪对象
扫描仪用户输入=新扫描仪(System.in);
做
{
//用户提供的提示计算器的填充值
System.out.println(“输入票据金额:”);
尝试
{
billAmount=userInput.nextDouble();
如果(billAmount<0)
抛出新异常(“账单金额不能为负数,请重试。”);
System.out.println(“输入您想要的小费百分比(20等于20%):”;
tipPercentage=userInput.nextInt();
如果(tipPercentage<0)
抛出新异常(“小费百分比不能为负。请重试。”);
System.out.println(“输入您的团队规模:”);
partySize=userInput.nextInt();
if(partySize<0)
抛出新异常(“聚会大小不能为负数。请重试。”);
}
捕获(例外e)
{
System.out.println(“输入无效。请重试。”+“\n”);
calculateTips();
}

我曾尝试使用InputMismatchType作为一个整体异常,但没有成功。正如您所看到的,我正在尝试在try块中显示这些自定义消息。如果有人能提供帮助,那就太好了。

问题是,您捕获了
异常
这意味着任何异常(以及excption的子类)将在同一块中捕获,解决方案是创建自定义异常并提前捕获,而不是将其他异常保留为默认值,例如:

try  {
  billAmount = userInput.nextDouble();
   if(billAmount < 0)
      throw new MyException("Bill amount can not be negative. Please try again."); 
} catch(MyException e) { // catch a specific exception first
 System.out.println("Exception" + e.getLocalizedMessage());
} catch(Throwable e) { // get all others to fall here, also using Throwable here cause it is also super for Exception and RuntimeException
 System.out.println("Invalid Input. Please try again." + "\n");
 calculateTips();
}
// and declare in the scope of class
class MyException extends Exception {
     MyException(String message) { super(message); }
}
试试看{
billAmount=userInput.nextDouble();
如果(billAmount<0)
抛出新的MyException(“账单金额不能为负数,请重试”);
}catch(MyException e){//首先捕获特定的异常
System.out.println(“异常”+e.getLocalizedMessage());
}catch(Throwable e){//让所有其他的都落在这里,在这里也使用Throwable,因为它对于异常和RuntimeException也是超级的
System.out.println(“输入无效。请重试。”+“\n”);
calculateTips();
}
//并在类的范围内声明
类MyException扩展了异常{
MyException(字符串消息){super(消息);}
}

另外,另一个解决方案是在catch中捕获特定的算术异常和parseException,并使用第三个异常处理特定错误(在这两种情况下仍然建议扩展一个)

你能解释一下
没有成功吗
?什么不成功?你看到了什么?你想看到什么?如果我没有正确理解,你想打印你捕获的异常消息。所以…不要打印
“无效输入。请再试一次。”
@markspace当我输入一个值时,就像字符串值应该是双精度的时候,我从我的整个catch语句中得到默认的“Invalid input”,而不是我需要显示的自定义异常。