阅读背景:

我在Java中抛出的异常不起作用

来源:互联网 

I'm working on a project where we are to create a cash register system that gives errors for orders of

I'm working on a project where we are to create a cash register system that gives errors for orders of $0.00 and 0 total items. The code for the exceptions is below. I have to use this method.

我正在开发一个项目,我们要创建一个收银系统,该订单系统会产生0.00美元和0件订单的错误。下面的例外代码如下。我必须使用这种方法。

  public boolean ValidateOrderTotal(double total)  
  {
      boolean validTotalFlag = true;
      try
      {
          if (total < 0)
          Exception invalidTotalEX = new Exception ("Total mst be >= $0.00");
          throw invalidTotalEX;
      }
  catch (Exception invalidTotalEX)(
  validTotalFlag = false;
  SetTotal(0.00);
  System.out.println(invalidTotalEX);
  }

return validTotalFlag;

public boolean ValidateOrderProductTotal (double totalItems)
{
boolean validProdctTotalFlag = true;
try
{
if (totalItems < 0)
(Exception invalidProductTotalEX = new Exception ("Product total must be >=0");
throw invalidProductTotalEX;
}
}
catch (Exception invalidProductTotalEX)(
validProdctTotalFlag = false);
SettotalItems (0);
system.out.println (invalidProductTotalEX);
)
return valid ProductTotalFlag

3 个解决方案

#1


2  

      if (total < 0)
      Exception invalidTotalEX = new Exception ("Total mst be >= $0.00");
      throw invalidTotalEX;

needs curly braces

需要花括号

      if (total < 0) {
          Exception invalidTotalEX = new Exception ("Total mst be >= $0.00");
          throw invalidTotalEX;
      }

You have a second if with the exact same issue.

如果遇到完全相同的问题,你有第二个问题。

Also your catch blocks need to use { and } around the statements. You are using ( and ) in some places.

你的catch块也需要在语句周围使用{和}。你在某些地方使用(和)。

#2


1  

As it throws error when it is 0 it should be:

因为它在0时抛出错误,它应该是:

if (total <= 0) {
          Exception invalidTotalEX = new Exception ("Total mst be > $0.00");
          throw invalidTotalEX;
      }

#3


0  

"catch (Exception invalidTotalEX)(" - there should be last '{' after "catch (Exception invalidTotalEX)(" instead of '('

“catch(异常invalidTotalEX)(” - 在catch之后应该有最后'{'(异常invalidTotalEX)(“而不是'('

Variable name does not matter, for example:

变量名无关紧要,例如:

viktor@Viks-pro:~/tmp/test $ cat ExTest.java
import java.util.*;
public class ExTest
{   public static void main(String[] args)
    {   try
        {   throw new Exception("Something should be different");
        }
        catch(Exception e)
        {   System.out.println("Exception: "+e.getMessage());
        }
    }
}viktor@Viks-pro:~/tmp/test $ java ExTest
Exception: Something should be different

.00 and 0 total items. The code for the exceptions is below. I have to use this method. I'm working on a project where we are to create




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: