【问题标题】:I can't get my method to throw a custom exception我无法让我的方法抛出自定义异常
【发布时间】:2015-02-11 23:12:50
【问题描述】:

我有一个 ProductionWorker 类和超类 Employee,这是我为学校项目创建的。我们应该在本周为其设置例外,但在尝试创建其中一个时我不断收到错误消息。这是我的代码:

在 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 而不是两个布尔值。

【问题讨论】:

  • 嗯...toString() 方法实际上不会抛出 InvalidShift 异常,所以你不应该声明它会,就这么简单。
  • InvalidShift 是自定义异常类,不是抛出异常的方法。
  • 我的错误。底线是:toString() 不应该被写成抛出任何异常。期间
  • 对于初学者,即使这是自定义异常类,也请遵守约定并适当命名:InvalidShiftException
  • 那么我应该在哪里抛出异常?我不能在 setShift() 方法中使用它,因为默认运行 setShift 方法会将 shiftValid 设置为 true。基本上我有两个布尔变量:dayShift 和 shiftValid。如果从未运行 setShift(),则 shiftValid 将为 false,如果有人尝试 getShift(),则应抛出异常。如果 dayShift 为 true,则表示此人从事白班工作,如果为 false,则表示此人上夜班。有意义吗?

标签: java exception methods superclass custom-exceptions


【解决方案1】:

底线是这样的:toString() 不应该被写成抛出任何异常。 Period 唯一的工作就是创建一个表示对象状态的字符串。它不应该改变对象本身的状态,也不应该抛出异常。

所以从toString() 中删除throws InvalidShift

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

你说:

现在,实际的异常本身是在 getShift() 方法中抛出的,

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

所以 Employee.toString() 方法实际上不可能抛出异常。

没错。所以它不应该像你在代码中那样被声明为抛出它。

程序会进入 ProductionWorker.toString(),运行 Employee.toString() 并返回它的字符串,然后再执行 getShift(),这可能会抛出异常。所以我不需要 Employee.toString() 能够抛出,如果这样做会导致其他问题。

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


是的,将其重命名为 InvalidShiftException


更多

我不能在 setShift() 方法中使用它,因为默认运行 setShift 方法会将 shiftValid 设置为 true。基本上我有两个布尔变量:dayShift 和 shiftValid。如果 setShift() 从未运行,则 shiftValid 将为 false,如果有人尝试 getShift(),则应抛出异常。

然后getShift() 应该被声明为抛出异常,事实上如果有人在对象处于无效状态时调用它(如果setShift() 没有被调用),实际上应该抛出它。

例如,

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

编辑
你说:

问题是 toString() 调用 getShift(),所以当抛出异常时 toString() 将在堆栈中。没有办法解决这个问题。

是的,有!在 toString() 中使用 try/catch!或在toString() 内部调用getShift() 之前先测试shift 是否有效(调用布尔方法)

【讨论】:

  • 问题是toString() 调用 getShift(),所以抛出异常时toString()会在栈中。没有办法解决这个问题。
  • @user2264356:是的,有!在 toString()! 中使用 try/catch!
  • 哦,行了。谢谢!
  • @user2264356: 或在调用getShift() 之前进行布尔检查。见编辑。
【解决方案2】:

你不能声明 toString() 来抛出 checked 异常,但是你可以声明它(或者直接抛出而不声明抛出)unchecked 异常——它们是子类RuntimeException.

将您的异常类更改为扩展 RuntimeException 而不是 Exception

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多