I am working on a fairly complex .NET application that interacts with another application. Many single-line statements are possible culprits for throwing an Exception and there is often nothing I can do to check the state before executing them to prevent these Exceptions.
The question is, based on best practices and seasoned experience, how frequently should I lace my code with try/catch blocks? I’ve listed three examples below, but I’m open to any advice.
I’m really hoping to get some pros/cons of various approaches. I can certainly come up with some of my own (greater log granularity for the O-C approach, better performance for the Monolithic approach), so I’m looking for experience over opinion.
EDIT: I should add that this application is a batch program. The only “recovery” necessary in most cases is to log the error, clean up gracefully, and quit. So this could be seen to be as much a question of log granularity as exception handling. In my mind’s eye I can imagine good reasons for both, so I’m looking for some general advice to help me find an appropriate balance.
Monolitich Approach
class Program{
public static void Main(){
try{
Step1();
Step2();
Step3();
} catch (Exception e) {
Log(e);
} finally {
CleanUp();
}
}
public static void Step1(){
ExternalApp.Dangerous1();
ExternalApp.Dangerous2();
}
public static void Step2(){
ExternalApp.Dangerous3();
ExternalApp.Dangerous4();
}
public static void Step3(){
ExternalApp.Dangerous5();
ExternalApp.Dangerous6();
}
}
Delegated Approach
class Program{
public static void Main(){
try{
Step1();
Step2();
Step3();
} finally {
CleanUp();
}
}
public static void Step1(){
try{
ExternalApp.Dangerous1();
ExternalApp.Dangerous2();
} catch (Exception e) {
Log(e);
throw;
}
}
public static void Step2(){
try{
ExternalApp.Dangerous3();
ExternalApp.Dangerous4();
} catch (Exception e) {
Log(e);
throw;
}
}
public static void Step3(){
try{
ExternalApp.Dangerous5();
ExternalApp.Dangerous6();
} catch (Exception e) {
Log(e);
throw;
}
}
}
Obsessive-Compulsive Approach
class Program{
public static void Main(){
try{
Step1();
Step2();
Step3();
} finally {
CleanUp();
}
}
public static void Step1(){
try{
ExternalApp.Dangerous1();
} catch (Exception e) {
Log(e);
throw;
}
try{
ExternalApp.Dangerous2();
} catch (Exception e) {
Log(e);
throw;
}
}
public static void Step2(){
try{
ExternalApp.Dangerous3();
} catch (Exception e) {
Log(e);
throw;
}
try{
ExternalApp.Dangerous4();
} catch (Exception e) {
Log(e);
throw;
}
}
public static void Step3(){
try{
ExternalApp.Dangerous5();
} catch (Exception e) {
Log(e);
throw;
}
try{
ExternalApp.Dangerous6();
} catch (Exception e) {
Log(e);
throw;
}
}
}
Other approaches welcomed and encouraged. Above are examples only.
5
Catch at the granularity that is meaningful to your application. If you are not going to take a different action for a different exception source, then there is no need to break those things apart.
3
I favor approach one.
The exceptions are going to go up the call stack to the try/catch
block in the main
method, and get logged there anyway.
Don’t catch
in your step
methods unless you can handle each exception locally somehow.
3
Ask yourself why exception syntax was invented when it’s perfectly possible to handle unexpected conditions without them. In C, all code looks like your obsessive-compulsive approach, except for instead of a try/catch
block, the return value of the function indicates success or failure.
The reason exceptions were invented is to allow code to look like your monolithic approach. It’s much cleaner, follows the DRY principle, and it’s easier to have people write the lower layers without imposing strict rules on exception handling in every single function. That should be your default mode of operation, and you should only also catch exceptions at the lower layers if you can actually recover from the error, not just report it.
1