抛出标准异常或创建并抛出自定义异常更好吗?

时间:2018-02-08 14:20:18

标签: java exception exception-handling

我和我的同事目前正在开发一个Java项目。当出于某种原因需要抛出异常时,其中一些会抛出一个标准的异常

        if ( !pduForConversion.isPresent())
        throw new Exception("Pdu optional is empty from get with oid "+oidForConversion.toString());

另一种方法是扩展Exception并为其提供自定义名称

    class emptyPduException extends Exception {
    public emptyPduException(String message) {
        super(message);
    }
}

      if (!getPduResponse.isPresent())
        throw new emptyPduException("Pdu optional is empty from get with oid " + convertedOid);

哪种方法被认为更一致?它们是等价的吗?

0 个答案:

没有答案