Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Basic question on exception handling in C++

Reply
Thread Tools

Basic question on exception handling in C++

 
 
masood.iqbal@lycos.com
Guest
Posts: n/a
 
      06-12-2007
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      06-12-2007
On 12 Juni, 15:04, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      06-12-2007
In comp.lang.c++ Erik Wikström <(E-Mail Removed)> wrote:
> On 12 Juni, 15:04, (E-Mail Removed) 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
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      06-13-2007
On 12 Juni, 19:45, (E-Mail Removed) (Marcus Kwok) wrote:
> In comp.lang.c++ Erik Wikström <(E-Mail Removed)> wrote:
>
>
>
> > On 12 Juni, 15:04, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Basic Exception Handling Handy C++ 0 04-13-2009 10:21 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments