18

I would like to know if there is an else statement, like in python, that when attached to a try-catch structure, makes the block of code within it only executable if no exceptions were thrown/caught.

For instance:

try {
    //code here
} catch(...) {
    //exception handling here
} ELSE {
    //this should execute only if no exceptions occurred
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
J3STER
  • 1,027
  • 2
  • 11
  • 28

2 Answers2

13

The concept of an else for a try block doesn't exist in c++. It can be emulated with the use of a flag:

{
    bool exception_caught = true;
    try
    {
        // Try block, without the else code:
        do_stuff_that_might_throw_an_exception();
        exception_caught = false; // This needs to be the last statement in the try block
    }
    catch (Exception& a)
    {
        // Handle the exception or rethrow, but do not touch exception_caught.
    }
    // Other catches elided.

    if (! exception_caught)
    {
        // The equivalent of the python else block goes here.
        do_stuff_only_if_try_block_succeeded();

    }
}

The do_stuff_only_if_try_block_succeeded() code is executed only if the try block executes without throwing an exception. Note that in the case that do_stuff_only_if_try_block_succeeded() does throw an exception, that exception will not be caught. These two concepts mimic the intent of the python try ... catch ... else concept.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
11

Why not just put it at the end of the try block?

Christoph S.
  • 323
  • 1
  • 2
  • 8
  • 1
    Just wanted to know if there were any, I dont know, it just makes the code nicer – J3STER Jan 25 '17 at 01:20
  • 2
    @J3STER disagree with that. Tacking on an "else" case defeats the point of having a single flow of code with exceptions totally ignored unless they do happen. Everything in the try block is "else" if no exception is thrown. – user4581301 Jan 25 '17 at 01:26
  • 3
    @J3STER: Why would that make the code nicer?! The existing exception mechanism already lets you do exactly what you want *without any extra structures*. – Kerrek SB Jan 25 '17 at 01:27
  • 16
    Python [document](https://docs.python.org/2.7/tutorial/errors.html) says, "it avoids accidentally catching an exception that wasn’t raised by the code being protected by the `try ... except` statement." I don't judge if it is good or not, it is just what the source says. –  Jan 25 '17 at 01:30
  • Nicky C is right, besides if you dont like it, then dont use it. Its better to have it there and not using it than wanting to use it but not being able because there is no such thing. Furthermore, the creators of nothing less than python thought it was a good idea to have such a statement available – J3STER Jan 25 '17 at 02:14
  • 3
    because the block of code you would put inside the else may cause an exception that you don't want to catch – J3STER Jan 25 '17 at 02:22
  • 1
    @J3STER - Python uses exceptions for flow control. Extreme example: Older versions of python routinely used `StopIteration` to signal the end of a loop. The C++ concept of exceptions is that they should only be used for exceptional events. Reaching the end of a loop is not an exceptional event. Every loop in C++ must end at some point; infinite loops are undefined behavior. – David Hammen Dec 22 '18 at 17:10
  • 1
    The python argument doesn't hold for another reason. Python is not a typed language, thus, `catch` simply catches all exceptions. However, C++ allows to catch different exception types in different catch blocks, which is not possible in python (at least not without testing the type at runtime and doing some hacks) – André Bergner Apr 24 '19 at 08:18
  • @J3STER That one language does something / has a specific feature does not mean that another language should do it / have that specific feature. C, for instance, has no such thing as an exception. And C has very good reasons for that. Even though exceptions are the quasi standard for error handling in many higher level languages, you don't want to implement that feature on a small, special purpose DSP that cannot pay for the stack-unwinding overhead. Likewise, you don't want any exceptions trolling around code like a system kernel, where error handling is at the core of the functionality. – cmaster - reinstate monica Apr 08 '20 at 14:50
  • @AndréBergner Not true, Python is strongly typed, but it's also Dynamically typed. In other words, object have well defined types, but variables can point to objects of different types. You can also catch different exception types in python using multiple except/catch statements same as you would do in C++. – Aaron de Windt Oct 25 '21 at 18:37
  • 1
    @AndréBergner The purpose of the else statement, is for code that you want to run after the try if there was no exception. Of course you could also put that code inside the try, but then the try/catch will also catch the exceptions of the same type(s) in that part of the code, which you might want to handle differently. Or not at all if you don't expect that exception will happen there. That way you'll receive an error if it actually happens. – Aaron de Windt Oct 25 '21 at 18:38
  • this "answer" should be a comment below the question. – Markus Dutschke Oct 28 '21 at 06:05