Are try/catch for every single statement that throws and exception considered an anti-pattern?

I can't serve you with an authoritative source, but these are fundamental tenets of exception handling.

I can't serve you with an authoritative source, but these are fundamental tenets of exception handling: exceptions should be caught at the point where one can properly handle them, and you must not swallow exceptions - there should always be (at least) a trace of an exception thrown, so the minimum is to log it, so that at least the developers get a chance of noticing that something bad happened. Which leads to the third point: exceptions should only be used to signal exceptional bad events, not to control program flow. There are lots of earlier posts on SO dealing with this, for example Best practices for exception management in JAVA or C#.

My initial reaction was that this is a bad thing. (Also I initially had arguments about catching exceptions in general, but removed those to focus on the example given in the question. ) Returning 0 on an exception is hard to judge here because it's unknown what bar and baz do, and what the exceptions represent.

If the caller really doesn't need to know anything went wrong, and the caller doesn't have to distinguish between the return value as error condition and return value as data, then this is ok. On the other hand if the exceptions thrown are problems with resources not being available it is better to fail fast than try to struggle on. I am doubtful this is a good thing because if it is reasonable for this code to return 0 on an exception, it would have been at least as reasonable for bar and baz to return 0 in the first place.To address catching exception for every single statement in general (as opposed to returning immediately with a default value as the example does), that is a bad thing because once you have an exception thrown, something has gone wrong and your method or object can be in a bad state.

The purpose of exception-handling is to have a means of escaping a context where something has gone wrong and getting back to a place with a known state. If catching every single exception and blundering on was ok, all our languages would support `On Error Resume Next'.

Whether returning 0 is a good idea or whether catching the exceptions or propagating them is appropriate is not what I am concerned about here. I agree that these are very important questions indeed, but my question is specifically on the use of lots of try/catchs for every single statement. The example is as I wrote just a constructed one.

– bjarkef Aug 17 at 13:26 @bjarkef: ok, your example code confused me because it is less virulent than the general case of eating exceptions and going on (which is what I see all over in places where I work lately :-( ), I was concerned I was not addressing your specific question. – Nathan Hughes Aug 17 at 13:34 Sorry about that. And yes, the general case of just eating exceptions is also very common here and definitely an anti-pattern (with possible huge consequences), but I know how to convince my co-workers of that being a wrong way to do it.

– bjarkef Aug 17 at 13:39 @bjarkef: I have an answer for the more general case that might be applicable, at stackoverflow. Com/questions/7001077/… – Nathan Hughes Aug 17 at 14:04.

The devil is in the detail. It depends on the context. When do you expect these exceptions?

For example if SomeException is indicative of a business rule violation that is quite common, and returning 0 is valid in that context, the I can see the above code being quite valid. Having the try..catch block close to the method that causes it is not a bad thing, as it could be quite a chore to work out which method could cause an exception if you wrap 10 methods in one giant try..catch. However, if these are exceptions that are indicative of something having gone very wrong and SomeException shouldn't occur, then returning "0" as a magic error indicator hides could potentially hide that something has gone wrong, the it could be a maintenance nightmare to find out where the problem occurred.

If the exception is not something that you can recover from, then you might as well put it in the throws clause or wrap it and rethrow it (if the exception is implementation specific, I wouldn't just put it in the throws clause as you wouldn't want to pollute a clean interface with Exceptions that are specific to the implementation). See also this SO article on checked vs unchecked exceptions which is applicable. Not logging an exception is also not necessarily a bad thing.

If the exception occurs very frequently and is not an indicator of something having gone terribly wrong, then you wouldn't want to write to the log every time as otherwise you'll "poison the logs" (i.e. Because if 99% of log entries are related to SomeException, then what's the likelihood that you'd miss an exception in the log and/or you run out of disk space because you get the log message 50,000 times a day).

For the given example, the argument is that it is redundant. You need just one try/catch block with just one return null.

I'm pretty sure the compiler would complain if you tried to return null from a method that returns the primitive type int. – Anthony Grist Aug 17 at 13:18 @Anthony: Yes, it is fixed. As I wrote it is just a constructed example.

– bjarkef Aug 17 at 13:40.

I think your colleague has read the old J2EE best practise "Catch an exception as close as possible to its source. " and then he went bit overboard with it.

I actually don't really think its an anti-pattern, per-se. But its definitely not clean code, which is almost as bad. The reason that I don't believe its an anti-pattern is that it doesn't seem like a good idea to begin with, which is a requirement for anything to be an anti-pattern.

Furthermore, this is a solution to coding problem whereas anti-patterns are general (bad) solutions to architectural problems.

Interesting point, but as a criteria it is quite subjective. Obviously for the OP's colleague it did seem like a good idea... Maybe we can refine it so: an antipattern is a solution to a problem in a context, which is believed to be good by many, but is fundamentally wrong nevertheless. – Péter Török Aug 17 at 13:21 @peter right its entirely subjective.To me it just seems like a bad coding style, most seasoned developers would not do this -- they would use one try/catch with multiple exception types and handle each type, or they would separate the tricky stuff into separate methods.

– hvgotcodes Aug 17 at 13:36 indeed, it looks like a bad idea to me too. I meant that the criteria for being an anti-pattern is subjective, not whether this particular solution is a good idea or not. IMO its badness can be defended by fairly objective arguments.

– Péter Török Aug 17 at 13:41 @Peter what do you mean? Regardless, I view this particular case the same as having 16 levels of nested ifs and loops. Its just not clean.

IMHO, bad code! = anti-pattern, but its just semantics. – hvgotcodes Aug 17 at 14:18 I can't seem to be able to clearly explain what I mean, sorry... I fully agree with you regarding the (un)cleanness of code.

I only meant to comment on your answer's last sentence, where you give a criteria for anti-patterns. – Péter Török Aug 17 at 14:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions