473,324 Members | 2,581 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,324 software developers and data experts.

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

Jun 12 '07 #1
3 2150
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
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
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 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...
19
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions...
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...
21
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.