|
||||
|
Section 17:
|
[17.7] I have too many try blocks; what can I do about it?
You might have the mindset of return codes even though you are using the syntax of try/catch/throw. For instance, you might put a try block around just about every call:
void myCode()
{
try {
foo();
}
catch (FooException& e) {
...
}
try {
bar();
}
catch (BarException& e) {
...
}
try {
baz();
}
catch (BazException& e) {
...
}
}
Although this uses the try/catch/throw syntax, the
overall structure is very similar to the way things are done with return
codes, and the consequent software development/test/maintenance costs are
basically the same as they were for return codes. In other words, this
approach doesn't buy you much over using return codes. In general, it is bad
form.
One way out is to ask yourself this question for each try block: "Why am I using a try block here?" There are several possible answers:
Main point is to ask "Why?". If you discover the reason you're doing it, you might find that there are better ways to achieve your goal. Having said all this, there are, unfortunately, some people who have the return-code-mindset burned so deeply into their psyche that they just can't seem to see any alternatives. If that is you, there is still hope: get a mentor. If you see it done right, you'll probably get it. Style is sometimes caught, not just taught. |
|||