作为一项规则,我尽量避免抛出 Exception 的实例,因为这并没有传达关于发生了什么问题的太多信息。
但我发现我得到了相当多的空异常类,它们看起来像这样......
class DataNotFoundException extends Exception {
// just a tagging class
}
Run Code Online (Sandbox Code Playgroud)
因此,该类在功能上与 Exception 相同。唯一的功能意义是我现在可以做到这一点......
try {
... some code which throws exceptions ...
} catch (DataNotFoundException $dnfe) {
... do stuff ...
} catch (OtherException $oe) {
... do other stuff ...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,拥有大量微小 Exception 类和仅抛出 Exception 实例之间的平衡点在哪里。有没有人对何时引入新的 Exception 类有任何指导?