C++ Else statement in Exception Handling
Asked Answered
M

2

20

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
}
Mccarthy answered 25/1, 2017 at 1:14 Comment(2)
No there isn't.Flexuous
@Someprogrammerdude - That should be the answer. The python concept of a try block with an else just doesn't exist in C++. It can be emulated, but no answer to date does so.Twocycle
T
16

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.

Twocycle answered 22/12, 2018 at 16:50 Comment(0)
B
12

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

Back answered 25/1, 2017 at 1:16 Comment(12)
Just wanted to know if there were any, I dont know, it just makes the code nicerMccarthy
@Mccarthy 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.Upthrow
@J3STER: Why would that make the code nicer?! The existing exception mechanism already lets you do exactly what you want without any extra structures.Ruggles
Python document 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.Lacefield
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 availableMccarthy
because the block of code you would put inside the else may cause an exception that you don't want to catchMccarthy
@Mccarthy - 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.Twocycle
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)Tallboy
@Mccarthy 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.Antiquity
@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++.Eupatrid
@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.Eupatrid
this "answer" should be a comment below the question.Forebear

© 2022 - 2024 — McMap. All rights reserved.