Basic question on exception handling in C++

M

masood.iqbal

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

Marcus Kwok

In comp.lang.c++ Erik Wikström said:
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top