By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

Basic question on exception handling in C++

P: n/a
In all the sample code snippets of try-catch code blocks that I have
seen, the catch block does one of the following three things:
1). exits the program (after spitting out a cerr message)
2). propagates the exception
3). throws yet another exception

I have a need to do something different. I want to merely spit out a
cerr message when I catch an exception, and then proceed with my
business logic. I am thinking of something like this:

////// Code snippet begin /////
bool noException = true;

try
{
// some business logic operation
}
catch(std::exception& xcptn)
{
noException = false;
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
<< __LINE__ << endl;
cerr << "Ignoring business logic operation\n";
}
catch(...)
{
noException = false;
cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
<< endl;
cerr << "Ignoring business logic operation\n";
}
if(noException)
{
// proceed with this business logic operation
}

////// Code snippet end /////

Am I doing it right, or is there a more professional way to do it?
Thanks,
Masood

Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 12 Juni, 15:04, masood.iq...@lycos.com wrote:
In all the sample code snippets of try-catch code blocks that I have
seen, the catch block does one of the following three things:
1). exits the program (after spitting out a cerr message)
2). propagates the exception
3). throws yet another exception

I have a need to do something different. I want to merely spit out a
cerr message when I catch an exception, and then proceed with my
business logic. I am thinking of something like this:

////// Code snippet begin /////
bool noException = true;

try
{
// some business logic operation}

catch(std::exception& xcptn)
{
noException = false;
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
<< __LINE__ << endl;
cerr << "Ignoring business logic operation\n";}

catch(...)
{
noException = false;
cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
<< endl;
cerr << "Ignoring business logic operation\n";

}

if(noException)
{
// proceed with this business logic operation

}

////// Code snippet end /////

Am I doing it right, or is there a more professional way to do it?
Seems good to me. If you wish you can put the business logic operation
within the try-block, after the thing that might throw, since no more
statements will be executed in the try block if an exception is
thrown.

--
Erik Wikström

Jun 12 '07 #2

P: n/a
In comp.lang.c++ Erik Wikström <er****@student.chalmers.sewrote:
On 12 Juni, 15:04, masood.iq...@lycos.com wrote:
>////// Code snippet begin /////
bool noException = true;

try
{
// some business logic operation}

catch(std::exception& xcptn)
{
noException = false;
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
<< __LINE__ << endl;
cerr << "Ignoring business logic operation\n";}

catch(...)
{
noException = false;
cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
<< endl;
cerr << "Ignoring business logic operation\n";

}

Seems good to me. If you wish you can put the business logic operation
within the try-block, after the thing that might throw, since no more
statements will be executed in the try block if an exception is
thrown.
One thing that I noticed is that your (the OP's) cerr output will always
output the same line number in the message, that is, the line number of
the cerr output statement. If you want to really see where the
exception occured, you need to encode it into the exception somehow at
the throw point.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 12 '07 #3

P: n/a
On 12 Juni, 19:45, ricec...@gehennom.invalid (Marcus Kwok) wrote:
In comp.lang.c++ Erik Wikström <eri...@student.chalmers.sewrote:
On 12 Juni, 15:04, masood.iq...@lycos.com wrote:
////// Code snippet begin /////
bool noException = true;
try
{
// some business logic operation}
catch(std::exception& xcptn)
{
noException = false;
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
<< __LINE__ << endl;
cerr << "Ignoring business logic operation\n";}
catch(...)
{
noException = false;
cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
<< endl;
cerr << "Ignoring business logic operation\n";
}
Seems good to me. If you wish you can put the business logic operation
within the try-block, after the thing that might throw, since no more
statements will be executed in the try block if an exception is
thrown.

One thing that I noticed is that your (the OP's) cerr output will always
output the same line number in the message, that is, the line number of
the cerr output statement. If you want to really see where the
exception occured, you need to encode it into the exception somehow at
the throw point.
Yes, I've been searching for a good solution to this but the best I've
come up with was declaring an exception-class like this:

class myException : public std::exception
{
myException(const char* msg, const char* file, long line);
};

And using a macros like this:

#define myException(x) myException(x, __FILE__, __LINE__)

So when I throw an exception I use the macro like this:

throw myException("Illegal value");

--
Erik Wikström

Jun 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.