mercredi 6 juin 2018

C++11 is Exceptions usage not safe?

I am new to C++ after using C# most of the time.

When writing a C++ code I did something like this:

if(unexpectedResult)
{
    throw std::runtime_error("failed because of ... "); 
}

void MyClass::MyFunction()
{
   try
   {
      ...
   }
   catch (const std::exception& e)
   {
       //report the error
   }
}

I was then told this is not safe to do in C++ compared to C#, and that I should convert my methods to return a Boolean to indicate a success or failure.

Why is it not safe? If it's not safe, then when do I use exceptions in C++? Does something changes if instead of throwing std::runtime_error, I'm throwing some custom exception I made?

Aucun commentaire:

Enregistrer un commentaire