f



Basic question on exception handling in C++

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

0
6/12/2007 1:04:36 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
337 Views

Similar Articles

[PageSpeed] 26

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 =3D true;
>
> try
> {
>   // some business logic operation}
>
>  catch(std::exception& xcptn)
> {
>   noException =3D false;
>   cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
> << __LINE__ << endl;
>   cerr << "Ignoring business logic operation\n";}
>
> catch(...)
> {
>   noException =3D 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=F6m

0
eriwik (511)
6/12/2007 2:19:58 PM
In comp.lang.c++ Erik Wikstr´┐Żm <eriwik@student.chalmers.se> wrote:
> 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
0
ricecake6398 (573)
6/12/2007 5:45:32 PM
On 12 Juni, 19:45, ricec...@gehennom.invalid (Marcus Kwok) wrote:
> In comp.lang.c++ Erik Wikstr=F6m <eri...@student.chalmers.se> wrote:
>
>
>
> > On 12 Juni, 15:04, masood.iq...@lycos.com wrote:
> >> ////// Code snippet begin /////
> >> bool noException =3D true;
>
> >> try
> >> {
> >>   // some business logic operation}
>
> >>  catch(std::exception& xcptn)
> >> {
> >>   noException =3D false;
> >>   cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
> >> << __LINE__ << endl;
> >>   cerr << "Ignoring business logic operation\n";}
>
> >> catch(...)
> >> {
> >>   noException =3D 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=F6m

0
eriwik (511)
6/13/2007 6:26:21 AM
Reply: