简体   繁体   English

Java在方法中引发异常的最佳方法

[英]Java Best way to throw exception in a method

I have created my own type of exception and want to implement it in a method. 我创建了自己的异常类型,并想在方法中实现它。 As of now I have written it in the following way, and it works. 到目前为止,我已经按照以下方式编写了它,并且可以正常工作。

public Worker remove (String firstName, String lastName, String number) throws NoSuchEmployeeException {
Worker w = null;
for (int i = 0; i < list.size(); i++) {
  if (list.get(i).getFirstName().compareTo(firstName) == 0 &&
      list.get(i).getLastName().compareTo(lastName) == 0 &&
      list.get(i).getNumber().compareTo(number) == 0) {
    w = list.get(i);
    list.remove(i);
  }
  else
    throw new NoSuchEmployeeException(/*"Employee could not be found"*/);
}
return w;
}

What I would like to know is if this is the best way to do it or if there is any other more appropriate/efficient/correct way of doing it. 我想知道的是,这是否是最好的方法,或者是否还有其他更合适/有效/正确的方法。 And also, do I need to declare the exception in the method header? 而且,我是否需要在方法标题中声明异常?

Thanks in advance. 提前致谢。

I'm not going to comment on whether or not to use checked vs unchecked exceptions as that will provoke a monster debate. 我将不评论是否使用检查与非检查异常,因为这将引起一场激烈的辩论。

If you create a checked exception, then yes it must be thrown in the method signature. 如果创建一个检查异常,则必须将其引发到方法签名中。 If you create an unchecked eg extends from RuntimeException then you do not need to throw it in the method signature. 如果创建了一个未选中的对象,例如从RuntimeException扩展,则无需将其放入方法签名中。

Checked exceptions are generally exceptions that are recoverable. 受检查的异常通常是可恢复的异常。 Unchecked exception are irrecoverable. 未经检查的异常是无法恢复的。

In case you have some other information other than error message to be sent out along with the exception. 如果您除了错误消息之外还有其他一些信息要与异常一起发送出去。

You will need to first create the Object of the Exception 您将需要首先创建异常的对象

set values you want to throw in the Exception 设置要抛出的异常值

Or you can write your own constructors for the Exception which would take different values and exception message to create object to the Exception you wanna throw. 或者,您可以编写自己的Exception构造函数,该构造函数将使用不同的值和异常消息来创建要抛出的Exception的对象。

As such 因此

throw new NoSuchEmployeeException(/*"Employee could not be found"*/);

is fine 很好

1 Your code would be more efficient if you implemented Comparable and the compareTo method in Worker as follows - 1如果您在Worker中实现了ComparablecompareTo方法,则您的代码将更加高效-

@Override
public int compareTo(Object obj) {
  // identity.
  if (obj == this) {
    return 0;
  } else if (obj instanceof Worker) {
    Worker w = (Worker) obj;
    if (w.getNumber().compareTo(number) != 0) {
      return w.getNumber().compareTo(number);
    } else if (w.getLastName().compareTo(lastName) != 0) {
      return w.getLastName().compareTo(lastName);
    }
    return w.getFirstName().compareTo(firstName);
  }
  return -1;
}

And then use the SortedSet collection type (eg TreeSet<Worker> ), especially the remove method. 然后使用SortedSet集合类型(例如TreeSet<Worker> ),尤其是remove方法。

2 You should probably just return null. 2您可能应该只返回null。 Throwing an Exception is certainly your choice, but (IMO) an unchecked Exception should be reserved for unrecoverable errors. 引发Exception当然是您的选择,但是(IMO)应该保留未经检查的Exception以保留不可恢复的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM