当我们得到一个实际上是异常的对象时,我们可以用它们做我们可以用我们语言中的普通对象做的任何事情.我们可以将它们作为参数传递,我们可以将它们存储在一些集合中,最糟糕的是,我们可以从方法中返回它们!
所以有人可能会写这样的臭代码:
public Exception doSomethingCritical()
{
Exception error = null;
try
{
...
}
catch (Exception e)
{
// maybe at least here is the logging of the error if we are lucky
error = e;
}
return error;
}
Run Code Online (Sandbox Code Playgroud)
所以问题是为什么Exception-object的概念是许多OO语言中的一等公民?也许如果我们在异常对象(如throw)允许的语言中只有有限的结构,那就更好了.