There is a try-catch thing about functions, which I think sometimes may be quite useful:
bool function()
try
{
//do something
}
catch(exception_type & t)
{
//do something
}
So the first part of the question: is this style considered bad in general case?
And the concrete example I used this approach in:
We had project with quite a lot of code in c and c++. And there we had custom exception types (not std::exception derived). I needed to integrate XML library and cast all exception to our types. So, basically, the last step was to catch all exceptions from XML library and convert them.
Function before:
bool readEntity(...)
{
while(...)
{
if(...)
{
//lot's of code...
}
}
}
after:
bool readEntity(...)
try
{
while(...)
{
if(...)
{
//lot's of code...
}
}
}
catch(XMLBaseException & ex)
{
//create our exception and throw
}
My thoughts went something like this: I clearly state my intentions to convert all exception derived from one type into custom type AND we keep our screen without horizontal scroll bar (cause horizontal scroll bars are bad).
Well, I actually was quite criticized for this approach as for non-clear one during code review.
So I'd like to hear you thoughts.
UPDATE: just to be clear: refactoring the function wasn't an option. And actually it was good written one.
Exceptions are not bad per se, but if you know they are going to happen a lot, they can be expensive in terms of performance. The rule of thumb is that exceptions should flag exceptional conditions, and that you should not use them for control of program flow.
Exception Handling: It's a Good Thing. There is nothing wrong with the previous code. But overusing these patterns will cause code smells, and won't necessarily be beneficial. Likewise, misusing them can actually do a lot of harm to your code base, making it brittle, or obfuscating the cause of errors.
As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.
The main reason C++ exceptions are so often forbidden is that it's very hard to write exception safe C++ code. Exception safety is not a term you hear very often, but basically means code that doesn't screw itself up too badly if the stack is unwound.
Really the only reason to function-level try blocks is for constructors, otherwise it's a somewhat obscure feature that doesn't buy you that much. It's just as easy to do it this way:
bool readEntity(...)
{
try
{
while(...)
{
if(...)
{
//lot's of code...
}
}
}
catch(XMLBaseException & ex)
{
//create our exception and throw
}
}
If you are having troubles with horizontal scrolling then the thing to do is to split up your code. try/catches are complexity and this should be represented in the nesting level, not hidden.
In constructors, this is a different issue: there is no other way to catch exceptions in an initializer list:
SomeClass::SomeClass(parameter p1, parameter p2) : Member1(p1), Member2(p2)
try
{
}
catch(Exception &ex)
{
// handle and rethrow
}
Of course, if you have an exception mid-construction, there's not likely much you can do to recover except log and and rethrow (it's going to get rethrown anyway in the constructor case). Your object isn't completely constructed yet and there's nothing you can really do with it. The only thing that you can trust to be valid are the parameters (although if the initialization failed, that will likely be due to bad parameters).
See this GOTW for a discussion on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With