mercredi 20 décembre 2017

rethorwing an exception in best practice

here is my function, I need to stop a simulator if an internal exception has occurred. my question is about my code vs other options

void SimulatorController::RunOnImage(std::list<simulator_input_image>&)
{
    try
    {
        InitSimulator();
        RunSimulator();

    }
    catch (const std::out_of_range& ex)
    {
        _isStopped = true;
        throw ex;
    }
    catch (const std::runtime_error& ex)
    {
        _isStopped = true;
        throw ex;
    }
    catch (std::exception& ex)
    {
        _isStopped = true;
        throw ex;
    }
}

I also noticed some people just use

catch (std::exception& ex)
        {
            _isStopped = true;
            throw;
        }

however this give compiler warnings (and I do not want those)

Also there is std::rethrow_exception()

what is consider to be the best practice? i'm using c++11

Aucun commentaire:

Enregistrer un commentaire