Exception rethrowing

Alexey Teplyakov

What is the difference between:

foo();

and

try{
    foo();
} catch (...){
    throw;
}

As I understand - if there is no try-catch block in function that calls foo() - the exception will be caught by try-catch block of outside function(if there is any). Am I right in this?

But what if there is a try-catch block in function that calls foo(), but there is no catcher who can handle it - must I write catch (...) { throw; } to let it be caught by someone outside it? Is it necessary?

masoud

A throw-expression with no operand rethrows the exception being handled. [§15.1/8]

So there are the same in practice.

the exception will be caught by try-catch block of outside function(if there is any). Am I right in this?

Yes.

But what if there is a try-catch block in function that calls foo(), but there is no catcher who can handle it - must I write catch (...) { throw; } to let it be caught by someone outside it? Is it necessary?

Handle exceptions which is expected you handle them in the calling point and leave the others. You don't have to re-throw them. If no catcher, catches the exception finally std:terminate will be invoked.

try
{
    foo();
}
catch (YourExpectedException &ex)
{
  // ...
}
catch (...)    \
{               \
                 > // You don't need this kind of re-throwing
    throw;      /
}              /

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exception rethrowing

From Dev

Error when rethrowing exception

From Dev

Exception rethrowing in java

From Dev

Level of exception handling / rethrowing an exception

From Dev

rethrowing exception handled in preceding catch block

From Dev

Sonar complaining about logging and rethrowing the exception

From Dev

Issue with a Spring @Async method not catching/rethrowing an exception

From Dev

rethrowing python exception. Which to catch?

From Dev

Is rethrowing an exception from catch block which has an nested exception okay?

From Dev

Rethrowing multiple instances of exception_ptr containing same exception

From Dev

C# - Thread Abort Exception (Thread Abort Exception) rethrowing itself

From Dev

Is rethrowing an exception from catch block which has an nested exception okay?

From Dev

Rethrowing multiple instances of exception_ptr containing same exception

From Dev

EJB - Unexpected exception was found while rethrowing pending exception

From Dev

Overhead of rethrowing Java exception vs. using instanceof/cast

From Dev

Rethrowing an Exception: Why does the method compile without a throws clause?

From Dev

Rethrowing exception in Task doesn't make the Task to go to faulted state

From Dev

Catching an Exception and rethrowing in C# - can it keep its type?

From Dev

Rethrowing SomeException

From Java

Rethrowing error in promise catch

From Dev

Rethrowing partially caught exceptions?

From Dev

c# - ThreadAbortException rethrowing

From Dev

Rethrowing exceptions in nested promise chains

From Dev

Immediately rethrowing in catch block and using finally

From Dev

Rethrowing strongly-typed WCF FaultException in a middle-layer service

From Dev

Throw exception or treat exception?

From Dev

Inheritance exception on exist exception

From Dev

System Exception or Application Exception

From Dev

Exception caused by other exception

Related Related

  1. 1

    Exception rethrowing

  2. 2

    Error when rethrowing exception

  3. 3

    Exception rethrowing in java

  4. 4

    Level of exception handling / rethrowing an exception

  5. 5

    rethrowing exception handled in preceding catch block

  6. 6

    Sonar complaining about logging and rethrowing the exception

  7. 7

    Issue with a Spring @Async method not catching/rethrowing an exception

  8. 8

    rethrowing python exception. Which to catch?

  9. 9

    Is rethrowing an exception from catch block which has an nested exception okay?

  10. 10

    Rethrowing multiple instances of exception_ptr containing same exception

  11. 11

    C# - Thread Abort Exception (Thread Abort Exception) rethrowing itself

  12. 12

    Is rethrowing an exception from catch block which has an nested exception okay?

  13. 13

    Rethrowing multiple instances of exception_ptr containing same exception

  14. 14

    EJB - Unexpected exception was found while rethrowing pending exception

  15. 15

    Overhead of rethrowing Java exception vs. using instanceof/cast

  16. 16

    Rethrowing an Exception: Why does the method compile without a throws clause?

  17. 17

    Rethrowing exception in Task doesn't make the Task to go to faulted state

  18. 18

    Catching an Exception and rethrowing in C# - can it keep its type?

  19. 19

    Rethrowing SomeException

  20. 20

    Rethrowing error in promise catch

  21. 21

    Rethrowing partially caught exceptions?

  22. 22

    c# - ThreadAbortException rethrowing

  23. 23

    Rethrowing exceptions in nested promise chains

  24. 24

    Immediately rethrowing in catch block and using finally

  25. 25

    Rethrowing strongly-typed WCF FaultException in a middle-layer service

  26. 26

    Throw exception or treat exception?

  27. 27

    Inheritance exception on exist exception

  28. 28

    System Exception or Application Exception

  29. 29

    Exception caused by other exception

HotTag

Archive