What guidlines do you use when deciding whether to let a method dodge an exception (ie: let the exception propogate up) or handle it once the exception is recieved?
Here is an example of what I am trying to ask
If I have three methods method1,2,3 and 3. Method1 calls Method2 which Calls Method3. and the exception is only thrown in method 3 when should I let the exception propogate upward as follows (excuse my pseudo java 😉 )
method1 { try { call method2; } catch (exception e) { doErrorProcessing; } } method2 throws exception { call method3; } method3 throws exception { call readFile; }
And when should I handle the exception once it is raised as follows
method1 { call method2; } method2 { call method3; } method3 { try { call readFille } catch (exception e) { doErrorProcessing; } }
The rule I follow:
If I can fix an exception, or nullify the problem that caused it (sometimes this means just ignoring it altogether), I’ll handle it. Otherwise it gets passed up to the next level.
And I always try to fix exceptions as low in the tree as possible (i.e. as soon as possible after they occur) – this localizes exception handling and avoids big honkin’ exception handlers in your upper levels.