在抛出旧异常时抛出新异常 [英] Throwing a new exception while throwing an old exception

查看:154
本文介绍了在抛出旧异常时抛出新异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在由异常引起的堆栈展开期间析构函数抛出C ++,程序将终止。 (这就是为什么析构函数不应该抛出C ++。)示例:

  struct Foo 
{
〜Foo ()
{
throw 2; // whoops,already throwing 1 at this point,let's terminate!
}
};

int main()
{
Foo foo;
throw 1;
}

在抛出'int'实例后调用终止

此应用程序已请求运行时以异常方式终止它。
请联系应用程序的支持团队以获取更多信息。

如果在Java中输入finally块,因为在相应的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()); // printssecond
}
}

:一个编程语言能否处理同时抛出的多个异常?这将有用吗?你有没有错过这种能力?有没有语言已经支持这个?

解决方案

从流量控制的角度考虑。异常基本上只是想要 setjmp / longjmp setcc callcc 无论如何。异常对象用于选择要跳转到的特定位置,例如地址。异常处理程序简单地递归当前异常 longjmp ,直到它被处理。



时间仅仅是将它们捆绑在一起成为一个的问题,使得结果产生相干流控制。我可以想到两个选择:




  • 将它们组合成一个不可预测的异常。它将相当于展开整个堆栈并忽略所有处理程序。这会造成异常级联导致完全随机行为的风险。

  • 不知何故构造其笛卡尔乘积。是,是的。



C ++方法有良好的可预测性。


If a destructor throws in C++ during stack unwinding caused by an exception, the program terminates. (That's why destructors should never throw in C++.) Example:

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.

If a finally block is entered in Java because of an exception in the corresponding try block and that finally block throws a second exception, the first exception is silently swallowed. Example:

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"
    }
}

This question crossed my mind: Could a programming language handle multiple exceptions being thrown at the same time? Would that be useful? Have you ever missed that ability? Is there a language that already supports this? Is there any experience with such an approach?

Any thoughts?

解决方案

Think in terms of flow control. Exceptions are fundamentally just fancy setjmp/longjmp or setcc/callcc anyway. The exception object is used to select a particular place to jump to, like an address. The exception handler simply recurses on the current exception, longjmping until it is handled.

Handling two exceptions at a time is simply a matter of bundling them together into one, such that the result produces coherent flow control. I can think of two alternatives:

  • Combine them into an uncatchable exception. It would amount to unwinding the entire stack and ignoring all handlers. This creates the risk of an exception cascade causing totally random behavior.
  • Somehow construct their Cartesian product. Yeah, right.

The C++ methodology serves the interest of predictability well.

这篇关于在抛出旧异常时抛出新异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆