473,411 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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?
Dec 16 '07 #1
10 1542
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
Dec 16 '07 #2
Hi,
Take a look at:
"C++ How to Program, Fifth Edition" By Dietel & Dietel.
Chapter 16... Exception Handling.
Dec 16 '07 #3
Any links to related content ?
? <ba********@yahoo.com?????? ??? ??????
news:3d**********************************@1g2000hs l.googlegroups.com...
Hi,
Take a look at:
"C++ How to Program, Fifth Edition" By Dietel & Dietel.
Chapter 16... Exception Handling.

Dec 17 '07 #4
On 2007-12-17 12:53, Hatzigiannakis Nikos wrote:
Any links to related content ?
? <ba********@yahoo.com?????? ??? ??????
news:3d**********************************@1g2000hs l.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
Dec 17 '07 #5
On 2007-12-17 00:34, ba********@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
Dec 17 '07 #6
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.
Dec 17 '07 #7
On 2007-12-16 14:10:50 -0500, Erik Wikström <Er***********@telia.comsaid:
>
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

Dec 17 '07 #8
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();
}
Dec 18 '07 #9
On 2007-12-17 20:25:53 -0500, john <jo**@no.spamsaid:
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

Dec 18 '07 #10
On Sun, 16 Dec 2007 18:16:30 +0200, Nikos Hatzigiannakis <yp**@aegean.grwrote:
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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Dec 18 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.