java - 我无法让我的方法抛出自定义异常

标签 java exception methods superclass custom-exceptions

我有一个 ProductionWorker 类,其父类(super class) Employee 是我为学校项目创建的。我们应该在本周为其设置异常(exception)情况,但在尝试创建其中之一时我不断收到错误。这是我的代码:

在 ProductionWorker 中:

   public String toString() throws InvalidShift{
      DecimalFormat dollar = new DecimalFormat("$#,##0.00");
      String str = super.toString();

      str += "The employee's pay rate is " + dollar.format(getPayRate()) +"\n";
      str += "The employee works the " + getShift() + " shift";
      return str;
   }

super.toString 类:

   public String toString() throws InvalidShift{
      String str = "The employee's name is " + getEmployeeName() + ".\n";
      if (employeeNumber.equals("")){
         str += "The employee's ID number is invalid.\n";
      }else{
         str += "The Employee's ID number is " + employeeNumber + ".\n";
      }
      str += "The employee's hire date was " + hireDate + ".\n";

      return str;
   }

当我尝试编译时,出现以下错误:

Employee.java:126: error: toString() in Employee cannot override toString() in Object
   public String toString() throws InvalidShift{
                 ^
  overridden method does not throw InvalidShift
ProductionWorker.java:54: error: toString() in ProductionWorker cannot override toString() in Object
   public String toString() throws InvalidShift{
                 ^
  overridden method does not throw InvalidShift

现在,实际的异常本身是在 getShift() 方法中引发的,因此 Employee.toString() 方法实际上不可能引发异常。该程序将进入 ProductionWorker.toString(),运行 Employee.toString() 并返回它的字符串,然后稍后执行 getShift(),这可能抛出异常。因此,我不需要 Employee.toString() 能够抛出异常,如果抛出异常,将会导致一系列其他问题。

有什么帮助吗?

编辑

我将把整个事情重写为一个 int 而不是两个 boolean 值。

最佳答案

底线是这样的:不应编写 toString() 来引发任何异常。 period 它唯一的工作就是创建一个表示对象状态的字符串。它不应该改变对象本身的状态,并且永远不需要抛出异常。

因此,请从 toString() 中删除 抛出 InvalidShift

相反,将此语句添加到实际上可能抛出异常的方法中。

您声明:

Now, the actual exception itself is thrown in the getShift() method,

所以应该声明这个方法来抛出异常。

so there's no way that the Employee.toString() method could actually throw the exception.

对。因此,它不应该像您在代码中所做的那样被声明为抛出它。

The program would enter the ProductionWorker.toString(), run the Employee.toString() and return it's string, then execute getShift() later, which could throw the exception. So I don't need Employee.toString() to be capable of throwing, and it would cause a mess of other problems if it did.

那么为什么首先要声明它抛出异常呢?

<小时/>

是的,将其重命名为 InvalidShiftException

<小时/>

更多

I can't have it in the setShift() method because running the setShift method by default sets shiftValid to true. Basically I have two boolean variables: dayShift and shiftValid. If setShift() is never run, shiftValid would be false and the exception should be thrown if someone attemts to getShift().

然后应该声明 getShift() 来抛出异常,并且实际上如果有人在对象处于无效状态时调用它,则应该抛出异常(如果 setShift() 例如尚未被调用)。

例如,

public Shift getShift() thows InvalidShiftException {
   if (!shiftValid) {
      throw new InvalidShiftException("maybe some text in here");
   } else {
      return whatIsSupposedToBeReturned;
   }
}
<小时/>

编辑
您声明:

the issue is that toString() calls getShift(), so toString() will be in the stack when the exception is thrown. There's no getting around that.

是的,有!在 toString() 内部使用 try/catch!或者先测试 shift 是否有效(调用 boolean 方法),然后再调用 toString()

内部的 getShift()

关于java - 我无法让我的方法抛出自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466389/

相关文章:

Java:Eclipse:找不到主类

c++ - 构造函数通过抛出异常结束?是否存在内存泄漏?

javascript - 如何在 Javascript 函数中引用数组方法

c++ - SFINAE 示例不起作用

java - 将字节数组从java程序发送到c++

java - 计算没有两个相邻字符相同的所有排列

java - 异常处理的2种方法

visual-studio-2008 - 捕获生成进程中的异常

C++ 在方法中传递 const 字符串引用?

java - UCanAccess加载数据库时HSQLDB "precision or scale out of range"错误