Throwing a new exception when throwing an old exception

If the destructor throws in C ++ while unwinding the stack caused by the exception, the program terminates. (This is why destructors should never throw in C ++.) Example:

struct Foo
{
    ~Foo()
    {
        throw 2;   // whoops, already throwing 1 at this point, let 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 support team for more information.

If the finally block is introduced in Java due to an exception in the corresponding try block and that finally the 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: can a programming language handle multiple exceptions at the same time? Would it be helpful? Have you ever missed this ability? Is there a language that already supports this? Is there any experience with this approach?

Any thoughts?

+5
source share
8

. setjmp/longjmp setcc/callcc. , , . , longjmp ing, .

- , . :

  • . . , .
  • - . , .

++ .

+5

. http://java.sun.com/docs/books/tutorial/essential/exceptions/chained.html

try {

} catch (IOException e) {
    throw new SampleException("Other IOException", e);
}

.

try{
}catch(Exception e){
}finally{
    try{
      throw new SampleException("foo");
    }catch(Exception e){
    }
}

Edit:

. , , - , . , , , (, ), . , ?

+3

? , , . ? , , . - , .

+2

, ; , , . , , ; , LINQ PLINQ , . , ... , "", " " , , , . , catch, , , , , , catch, .

0

++ std:: exception_ptr . , , . , .

0

, , JUnit:

  • , ( , ).
  • @After , .
  • After , .
  • , After, IDE (Eclipse) .

, JUnit , Eclipse , JUnit, .

, Eclipse , . " ", finally try, .

0

, Exception("First") Exception("second"), . , , , ...

0

, , "" , , . , "" , - . , . "" , , , , . , " ", , , catch, "" .

0
source

All Articles