Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ trivial try-catch causes abort

Tags:

c++

the simple code below

// g++ centro.cc -o centro

#include <iostream>
using namespace std;

int  main(int argc, char *argv[])
{
    try
    {
        cout << "Going to throw" << endl;
        throw;
    }
    catch(...)
    {
        cout << "An exception occurred" << endl;
    }
    return 0;
}

produces an abort:

Going to throw
terminate called without an active exception
Aborted (core dumped)

I don't understand what's wrong, can anybody point me in the right direction?

like image 343
giuspen Avatar asked Jul 03 '12 10:07

giuspen


People also ask

Does Try Catch affect performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Do try catch blocks hurt performance C#?

Still, there are some situations where the existence of a try/catch block may prevent optimizations which--but for the try/catch--would have allowed code to run faster. Save this answer. Show activity on this post. Yes, try/catch will "hurt" performance (everything is relative).

Does try catch make code slow?

Originally Answered: Does using "try and catch" in programming makes the run of the code slower? Yes, the mechanism added in try ..


1 Answers

Try thowing something. You aren't throwing any exception.

throw; itself is generally used to re-throw the same exception inside a catch block.

Compare the result with throw "something"; or perhaps an instance of std::exception.

like image 54
Kos Avatar answered Sep 20 '22 20:09

Kos