Removing excessive try-catch blocks
"To log exceptions appropriately and prevent them from propagating to the user, have an Application.ThreadException handler"
Would you then be able to tell the user what happened? Would all exceptions end up there?
"For cases where there's a resource that needs cleanup, leave the try-catch block as it is"
You can use try-finally
blocks as well if you wish to let the exception be handled elsewhere. Also consider using the using
keyword on IDisposable
resources.
"In methods that "return-false-on-error", let the exception propagate and catch it in the caller instead"
It depends on the method. Exceptions should occur only in exceptional situations. A FileNotFoundException
is just weird for the FileExists()
method to throw, but perfectly legal to be thrown by OpenFile()
.
For cleanup rather use try-finally
or implement the IDisposable
as suggested by Amittai. For methods that return bool on error rather try and return false if the condition is not met. Example.
bool ReturnFalseExample() {
try {
if (1 == 2) thow new InvalidArgumentException("1");
}catch(Exception e) {
//Log exception
return false;
}
Rather change to this.
bool ReturnFalseExample() {
if (1 == 2) {
//Log 1 != 2
return false;
}
If i'm not mistaken try catches
are an expensive process and when possible you should try determine if condition is not met rather then just catching exceptions.
}