Sign in to follow this  

C++ exceptions

This topic is 3339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all As a C programmer I was accustomed to the following method of error handling: if (something went wrong) { // cleanup printf("blablabla\n"); exit(1); } Can someone provide me with the equivalent piece of code in C++? I assume that it will use the try-throw-catch sceme? I have experimented a little, but something is wrong and I cannot locate it. The following program, ------------------------------------------------------------------------ #include <iostream> using namespace std; class foo { private: int i; public: foo(int); ~foo(); void my_throw(); }; foo::foo(int a) { i=a; cout<<"Creating a foo with a value of "<<i<<endl; } foo::~foo() { cout<<"Destroying a foo with a value of "<<i<<endl; } void foo::my_throw() { // if (an unrecoverable error) throw; } main() { foo a(3); a.my_throw(); } -------------------------------------------------------------------------- when executed, fails to call the destructor: --------------------------------------------------------------------------- autir@thrall:~/my_documents/practice$ ./a.out Creating a foo with a value of 3 terminate called without an active exception Aborted --------------------------------------------------------------------------- Any ideas?

Share this post


Link to post
Share on other sites
You have to specify something to throw. Normally, this will be an object of some class derived from std::exception. "throw" by itself is only used within catch clauses, to re-throw what was caught.

Share this post


Link to post
Share on other sites
It's worth noting that returning an error value isn't necessarily the wrong thing to do. Sometimes it's more appropriate than throwing an exception.

If you're down in a deep call and you have the ability to create an error object that would be meaningful to code several tiers up the call-stack, an exception would be appropriate.

Also, anyone that writes code that throws exceptions, or uses code that throws exceptions, should be aware of RAII.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
"throw" by itself is only used within catch clauses, to re-throw what was caught.


What happens when it is used by itself? Isn't a default handler called? How can I manipulate/change this handler?

Quote:
Original post by the_edd
If you're down in a deep call and you have the ability to create an error object that would be meaningful to code several tiers up the call-stack, an exception would be appropriate.


I have slightly modified the sample program. Still cannot achieve stack unwinding.

----------------------------------------------------------------------------
#include <iostream>

using namespace std;

class foo
{
private:
int i;
public:
foo(int);
~foo();
void my_throw();
};

foo::foo(int a=0)
{
i=a;
cout<<"Creating a foo with a value of "<<i<<endl;
}

foo::~foo()
{
cout<<"Destroying a foo with a value of "<<i<<endl;
}

void foo::my_throw()
{
// if (an unrecoverable error)
throw;
}

void bar(int i)
{
foo a(i);
a.my_throw();
}

main()
{
bar(3);
}
---------------------------------------------------------------------------
autir@thrall:~/my_documents/practice$ ./a.out
Creating a foo with a value of 3
terminate called without an active exception
Aborted
---------------------------------------------------------------------------

Why isn't ~a() executed after leaving bar() via the throw;

Share this post


Link to post
Share on other sites
Quote:
Original post by autir
What happens when it is used by itself?
In a catch clause (or a function called from a catch clause), it throws the caught exception. Outside, it will generate a runtime error.

Quote:
I have slightly modified the sample program. Still cannot achieve stack unwinding.
This is perfectly normal. Stack unwinding only happens when you throw an exception. However, your code executes throw;, notices that there is no caught exception (since you're not inside a catch clause or a function called from one), and aborts the program without throwing anything.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
This is perfectly normal. Stack unwinding only happens when you throw an exception. However, your code executes throw;, notices that there is no caught exception (since you're not inside a catch clause or a function called from one), and aborts the program without throwing anything.

Just a slight technical correction: stack unwinding only occurs when you catch an exception. The default action if you throw an exception that is not caught is to call std::terminate(), which by default calls ::abort(), which will exit the process with prejudice (that is, without unwinding the stack).

So, even if the OP did throw an exception (instead of just executing throw), he's not going to see his destructor executed unless he has a catch clause in main().

Share this post


Link to post
Share on other sites

This topic is 3339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this