我和我的同事目前正在开发一个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);
哪种方法被认为更一致?它们是等价的吗?