What happens after you catch the exception? Can you refactor? Often you can, in concept, put the try catch at a different level.
processUserRequest() {
try {
if (function1() )
function2()
function3()
... etc
prepare success response to user
} catch (TimeOutException ) {
log message
prepare fail response to user
} catch ... etc
}
send response
}
Also is it possible to reduce the number of exceptions you need to catch? In Java I tend to use hierarchies of exceptions. I have a TransientException, thrown when its reasonable to expect a retry of the same request to work. Timeout and Communication exceptions are subclasses of TransientException, so I only need to catch TransientrException. Similarly I have an InvalidRequestException - you can make an invalid request as many times as you like but it will never work! So I can then subclass InvalidRequestException for cases such as malformed SQL.
In response to your further explanation:
Your intent is to wrap each function so that the caller can examine a success code instead of catch an exception. The desire is that the caller will code
Response result = function1( ... ) ;
if ( result.isError() ) {
do something with result.reason();
} else {
maybe do something with the answer
}
First let me say, why bother? They can catch all Exceptions with one block.
try {
function1( ... )
maybe do somethign with the annswer
}
catch(Exception e) {
callExceptionReporter(e);
}
this in my eyes is much clearer, the flow of control is explicit. It's no more lines of code and is much more maintainable. There seems to be general opinion that exceptions make code unclear - I just do not agree.
NOTE: The idea of catching Exception has caused some debate. Let me clarify a couple of points.
Catching Exception is not in itself evil, but it is very important that if you do catch Exception you do the right thing. The issues are discussed in this question.
I do not advocate silenty swallowing any exception, least of all important system exceptions. For each caught exception one needs to decide whether to log a message, often logging is the minumum correct thing to do. In the case under consideration we are catching Exception so that we can factor the common processing. It's not the usual thing to do, but catching that Exception is not intrinsically wrong, providing that we do the right thing. Do not blindly follow rules such as "do not catch Exception".
The implementation of callExceptionReporter() has responsibility for doing the right thing. For the application-specific exceptions can prepare the error result. For the other exceptions it can simpy rethrow.
Certain exceptions are not catchable. Example From .Net 2.0 StackOverflowException is not (usually) catchable. This makes no difference to our code, we don't catch it, we don't want to catch it.
There have been suggestions that by catching Exception we may open up some possibility of finally blocks misbehaving. This is simply not the case. Finally blocks fire long before our Exception handler is reached. If finallys misbehave it's not dues to the use of this catch. If we reach this catch because a finally block threw an exception, no matter its just another exception to process or rethrow.
Returning to your original question If you want/need to go ahead with your plan, then as has been observed, factor the actual Exception processing to its own function. You are then writing minimal repeated code.
try {
return function1( ... )
}
catch(Exception e) {
return prepareExceptionResponse(e);
}
So long as you do the right thing in prepareExceptionResponse(), rethrowing low level exceptions you are not doing any harm. It's unusual to cathc Exception at this level but you have a special need - to do some common exception processing.