Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ exception handling (http://www.velocityreviews.com/forums/t561527-c-exception-handling.html)

Nikos Hatzigiannakis 12-16-2007 04:16 PM

C++ exception handling
 
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?



Erik Wikström 12-16-2007 07:10 PM

Re: C++ exception handling
 
On 2007-12-16 17:16, Nikos Hatzigiannakis wrote:
> 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.

--
Erik Wikström

babakandme@yahoo.com 12-16-2007 11:34 PM

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

Hatzigiannakis Nikos 12-17-2007 11:53 AM

Re: C++ exception handling
 
Any links to related content ?
? <babakandme@yahoo.com> ?????? ??? ??????
news:3d042ace-23e6-47c3-a3c9-a0bce6f1cb14@1g2000hsl.googlegroups.com...
> Hi,
> Take a look at:
> "C++ How to Program, Fifth Edition" By Dietel & Dietel.
> Chapter 16... Exception Handling.




Erik Wikström 12-17-2007 07:23 PM

Re: C++ exception handling
 
On 2007-12-17 12:53, Hatzigiannakis Nikos wrote:
> Any links to related content ?
> ? <babakandme@yahoo.com> ?????? ??? ??????
> news:3d042ace-23e6-47c3-a3c9-a0bce6f1cb14@1g2000hsl.googlegroups.com...
>> Hi,
>> Take a look at:
>> "C++ How to Program, Fifth Edition" By Dietel & Dietel.
>> Chapter 16... Exception Handling.


www.amazon.com

--
Erik Wikström

Erik Wikström 12-17-2007 07:25 PM

Re: C++ exception handling
 
On 2007-12-17 00:34, babakandme@yahoo.com wrote:
> 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.

--
Erik Wikström

john 12-17-2007 08:37 PM

Re: C++ exception handling
 
Nikos Hatzigiannakis wrote:
>
> 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.

Kira Yamato 12-17-2007 09:02 PM

Re: C++ exception handling
 
On 2007-12-16 14:10:50 -0500, Erik Wikstrm <Erik-wikstrom@telia.com> said:

>
> 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();
}

--

-kira


john 12-18-2007 01:25 AM

Re: C++ exception handling
 
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();
}

Kira Yamato 12-18-2007 10:27 AM

Re: C++ exception handling
 
On 2007-12-17 20:25:53 -0500, john <john@no.spam> said:

> 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.

--

-kira



All times are GMT. The time now is 06:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.