Carlinha Santana Carlinha Santana - 3 months ago 11
C++ Question

How can I handle exceptions in C++ in this code?

int divide_numbers(int a, int b)
{
return a/b;
}


How can I handle exceptions in C++? Please, I am newbie and I need a simple code example in C++! Could someone help me?

Answer

Maybe, this can help:

  int divide_numbers(int a, int b)
{
    try
    {
        if(b==0)
            throw 1;

        return a/b;
    }
    catch(int err_code)
    {
        cout << "ERROR CODE " << err_code;
    }
}
Comments