Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::exception catch my exception before std::bad_alloc?

Tags:

c++

exception

Problem : I am using both std::exception and std::bad_alloc to catch exception. Something is wrong with the order of the try catch that I am using. I attached sample code for reference.

Expected : If my error is bad_alloc then the bad_alloc exception is thrown.

Observed : My error is bad_alloc, but exception is thrown.

Sample Code :

#include "stdafx.h"
#include <iostream>
#include <exception>

using namespace std;

void goesWrong()
{
    bool error1Detected = true;
    bool error2Detected = false;

    if (error1Detected)
    {
        throw bad_alloc();
    }

    if (error2Detected)
    {
         throw exception();
    }
}

int main()
{
    try
    {
        goesWrong();
    }
    catch (exception &e)
    {
        cout << "Catching exception: " << e.what() << endl;
    } 
    catch (bad_alloc &e)
    {
        cout << "Catching bad_alloc: " << e.what() << endl;
    }

    return 0;
}
like image 406
Evan Gertis Avatar asked Feb 05 '18 09:02

Evan Gertis


People also ask

What can cause std :: Bad_alloc?

std::bad_alloc is a type of exception that occurs when the new operator fails to allocate the requested space. This type of exception is thrown by the standard definitions of ​operator new (declaring a variable) and operator new[] (declaring an array) when they fail to allocate the requested storage space.

What is std :: Bad_alloc C++?

std::bad_allocType of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space. This class is derived from exception .

How do you deal with bad Alloc?

The only thing you could do when catching std::bad_alloc is to perhaps log the error, and try to ensure a safe program termination by freeing outstanding resources (but this is done automatically in the normal course of stack unwinding after the error gets thrown if the program uses RAII appropriately).

What is std :: exception in C++?

std::exception::whatReturns a null terminated character sequence that may be used to identify the exception. The particular representation pointed by the returned value is implementation-defined. As a virtual function, derived classes may redefine this function so that specific values are returned.


1 Answers

You have to put your exceptions in reverse order, regarding their inheritance relationship. std::exception is the parent class of std::bad_alloc, that is why it is found before in the catch list. So you have to transform your code to be:

   try {
      goesWrong();
   }
   catch (bad_alloc &e)
   {
      cout << "Catching bad_alloc: " << e.what() << endl;
   }
   catch (exception &e)
   {
      cout << "Catching exception: " << e.what() << endl;
   }

You're not limited to catch objects: you can throw integers, chars... whatever. In that case, catch(...) is the only secure way to catch them all.

That said, using objects from the standard class library is the advised way to do it. And in this case, since std::exception is the base class for all (standard) exceptions, it will catch all possible exceptions thrown.

You can create your own exception classes deriving them from std::exception, or from std::runtime_error, for example, my personal choice.

Hope this helps.

like image 102
Baltasarq Avatar answered Nov 16 '22 01:11

Baltasarq