A code base I have inherited is riddled with the error-hiding anti-pattern:
public void foo() {
try {
//Entire body of method, sometimes only 5 lines but often 500+
} catch (Exception e) {
Logger.LogToFile(e.msg);
}
}
The number of instances of this pattern are in the order of hundreds.
My approach was to just remove the try..catch block entirely, given we already have a single exception-handler at the top level. This was unpopular as it was felt I was changing behaviour. (More exceptions would bubble up).
Ideally, I would go through each of the 100s of instances of 10-500 lines and find what exceptions could possibly be thrown. Then I'd only squash those exceptions in tighter blocks:
public void foo() {
//...
try {
// ~1-5 lines
} catch (ArgumentInvalidException e) {
Logger.LogToFile(e.ToString());
//TODO handle
}
//...
try {
// ~1-5 lines
} catch (UnitConversionException e) {
Logger.LogToFile(e.ToString());
//TODO handle
}
//...
}
This is an intimidating amount of effort.
Can anyone think of a clever approach to fixing this?
Related - Removing excessive try-catch blocks