如果一个析构函数在由异常引起的堆栈展开期间抛出C++,程序将终止。 (这就是为什么析构函数不应该用C扔++。)例:抛出一个新的异常抛出一个旧的异常
struct Foo
{
~Foo()
{
throw 2; // whoops, already throwing 1 at this point, let's terminate!
}
};
int main()
{
Foo foo;
throw 1;
}
terminate called after throwing an instance of 'int'
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
如果finally块被输入在Java中,因为在相应try块的异常,并且finally块抛出了第二个例外,第一个例外是默默吞下。例如:
public static void foo() throws Exception
{
try
{
throw new Exception("first");
}
finally
{
throw new Exception("second");
}
}
public static void main(String[] args)
{
try
{
foo();
}
catch (Exception e)
{
System.out.println(e.getMessage()); // prints "second"
}
}
这个问题在我脑海中浮现:编程语言是否可以处理同时抛出多个异常?这会有用吗?你有没有错过这个能力?有没有一种语言已经支持这个?有没有这种方法的经验?
有什么想法?
你刚刚让我的大脑抛出异常 – 2010-04-26 20:51:01
有趣的问题。我通过“处理异常”来假设你明确地意思是“由于异常导致堆栈展开”,而不是“从catch块执行代码”。后者我会称之为“处理异常”,但是由于处理程序已经定位,所以可以从那里抛出一个异常(至少在C++中)。 – 2010-04-26 20:57:22
@尼克你是对的,我编辑了标题。如果你知道更好的一个,随时可以再次更改;-) – fredoverflow 2010-04-26 20:59:19