C++ exception handling

  • Thread starter Nikos Hatzigiannakis
  • Start date
N

Nikos Hatzigiannakis

I am trying to understand how to use an exception handler to heal from a
given exception, and not just display a message!



Since an exception is raised the control of the program is transferred to
the proper catch clause. What can we do after displaying a message to
continue the normal operation of the program?
 
E

Erik Wikström

I am trying to understand how to use an exception handler to heal from a
given exception, and not just display a message!



Since an exception is raised the control of the program is transferred to
the proper catch clause. What can we do after displaying a message to
continue the normal operation of the program?

It of course depends on why there was an exception to begin with. If the
exception was raised due to a transient error then you can retry:

bool succeed = false;

while (!succeed)
{
try
{
doSomelthingThatMightThrow()
succeed = true;
}
catch(SomeException& e)
{
logFailure();
}
}

In other cases you might try a different approach:

int result;
try
{
result = firstApproach();
}
catch(Exception& e)
{
result = secondApproach();
}

There is no simple answer, since it all depends on what you are trying
to do and the reasons for the exceptions.
 
B

babakandme

Hi,
Take a look at:
"C++ How to Program, Fifth Edition" By Dietel & Dietel.
Chapter 16... Exception Handling.
 
E

Erik Wikström

Hi,
Take a look at:
"C++ How to Program, Fifth Edition" By Dietel & Dietel.
Chapter 16... Exception Handling.

If you want to reply to the OP then please reply to the OP and to my
reply to the OP. Also please quote the text you are replying to.
 
J

john

Nikos said:
>
I am trying to understand how to use an exception handler to heal from a
given exception, and not just display a message!

Can you become more specific to yoir issue? There are many ways you can
handle exceptions, for example you can place the entire body of a
function in a try block, like this:


void g() try
{
// function body
}

catch(const exception &e)
{
// Do stuff
}



Since an exception is raised the control of the program is transferred to
the proper catch clause. What can we do after displaying a message to
continue the normal operation of the program?

You can retry the operation or whatever.
 
K

Kira Yamato

bool succeed = false;

while (!succeed)
{
try
{
doSomelthingThatMightThrow()
succeed = true;
}
catch(SomeException& e)
{
logFailure();
}
}

A bit off topic, but must you utilize the flag variable succeed? That
approach feels so C-like.

Rather, how about the following version?

while(1)
try
{
doSomething();
break;
}
catch(SomeException &e)
{
fixProblem();
}
In other cases you might try a different approach:

int result;
try
{
result = firstApproach();
}
catch(Exception& e)
{
result = secondApproach();
}

Err... For multiple approaches, I rather recommend the following
more-symmetrical structure instead:

for(;;)
{
try { approach1(); break; }
catch(SomeException &e) { logFailure(); }

try { approach2(); break; }
catch(SomeException &e) { logFailure(); }

try { approach3(); break; }
catch(SomeException &e) { logFailure(); }

throw AllAproachFailedException();
}
 
J

john

Consider the following working example:


#include <iostream>
#include <exception>

inline void g() try
{
throw std::exception();
}


catch(const std::exception &)
{
std::cout<< "Exception received!\n";
g();
}


int main()
{
g();
}
 
K

Kira Yamato

Consider the following working example:


#include <iostream>
#include <exception>

inline void g() try
{
throw std::exception();
}


catch(const std::exception &)
{
std::cout<< "Exception received!\n";
g();
}


int main()
{
g();
}

Neat. The syntax shows elegantly that it can do re-tries.

It would've been practical too if C++ supports tail-recursion.
 
J

Jorgen Grahn

I am trying to understand how to use an exception handler to heal from a
given exception, and not just display a message!
Since an exception is raised the control of the program is transferred to
the proper catch clause. What can we do after displaying a message to
continue the normal operation of the program?

The same things as if you passed an error code back instead of
throwing an exception.

The main difference is, I guess, if you use exceptions you can design
your code to handle cleanup automatically, as the exception floats up
the call chain towards the catch clause.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top