Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does throwing and catching ints work?

With this code:

int main()
{
    try
    {
        throw -1;
    }
    catch (int& x)
    {
        std::cerr << "We caught an int exception with value: " << x << std::endl;
    }
    std::cout << "Continuing on our merry way." << std::endl;

    return 0;
}

We have:

/tmp$ ./prorgam.out
Continuing on our merry way
We caught an int exception with value: -1

How does the catch block read -1 as int&? We couldn't assign a value to a non-const lvalue reference.

And why is the second std::cout statement executed before the first std::cerr statement?

like image 394
Ghasem Ramezani Avatar asked Dec 19 '19 14:12

Ghasem Ramezani


2 Answers

This is okay because of [except.throw]/3

Throwing an exception copy-initializes ([dcl.init], [class.copy.ctor]) a temporary object, called the exception object. An lvalue denoting the temporary is used to initialize the variable declared in the matching handler ([except.handle]).

emphasis mine

As you can see, even though it is a temporary, the compiler treats it as an lvalue for initializing the handler. Because of this, you don't need a const reference.

like image 124
NathanOliver Avatar answered Nov 01 '22 10:11

NathanOliver


From this throw reference:

Unlike other temporary objects, the exception object is considered to be an lvalue argument when initializing the catch clause parameters, so it can be caught by lvalue reference, modified, and rethrown.

So while the "object" is temporary, it's still an lvalue and as such you can catch it by reference.

like image 29
Some programmer dude Avatar answered Nov 01 '22 10:11

Some programmer dude