While the above is a pedantic example of how to use try/catch, it is not recommended to "handle" the type Exception. There are many reasons to avoid handling Exception type:
- It hides bugs
By not producing any error to the user you don't give them any feedback that anything is wrong and therefore the existence of the bug does not manifest immediately.
- Handling all exceptions without exception-specific handling logic will leave the application in an unknown state.
The exception was thrown for a reason. If you don't know how to handle it then you don't know the consequences of the exception that occurred. Those consequences may leave the application in an unknown state that will affect subsequent processing, ending up with the above--hidden bugs.
- It circumvents higher-level exception handling.
A higher-level exception handler may know exactly what to do with a certain type of exception; possibly much better than your code. By using catch(Exception) you circumvent that and end up with the above--the application may be in a unknown state.
"Handling" and "catching"
"Handling" an exception is the process of catching an exception and not rethrowing the exact caught exception. It is quite permissible to perform clean-up code in the event something went wrong, which could be as simple as:
try
{//...
}
catch(Exception)
{ file.Close();
throw; // rethrow the exact exception
}
With languages that support the "finally" keyword this is generally not necessary.
When to handle "Exception"
In a top-level, or last-chance, exception handler (one where catch is the last statement before termination of the application), it is safe to "handle" the exception when logging uncaught exceptions, there is a need to provide a more user-friendly message, or application-specific information needs not to be displayed. For example
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e.ToString());
MessageBox.Show("An fatal error in the application occured and it had to be terminated");
}
}
General exception handling guidelines
- Do not catch general exception types like System.Exception and System.SystemException
- Choose exceptions over error codes.
- Do not use exception handling for flow of control, only for failure situations.
- Document all exceptions thrown by a publicly callable members. (see exception element in XML documentation comments).
- Do not have publicly callable members throw or not throw based upon a parameter.
- Do you use exceptions as return values or out parameters (this is an error code).
- Avoid throwing exceptions from finally blocks.
- Avoid throwing exceptions from constructors.
- Do not rethrow by throwing the same exception object. This causes the stack trace for the original exception to be lost--use a lone "throw;" statement (without an object following it) to "rethrow" a catch exception.
References
.NET Framework Developer's Guide: Catching and Throwing Standard Exception Types[http://msdnwiki.microsoft.com/en-us/mtpswiki/ms229007.aspx]
Visual Studio Team System Code Analysis: Do not catch general exception types [http://msdn2.microsoft.com/en-us/library/ms182137.aspx]
FxCop FAQ: Why does FxCop warn against catch(Exception)? discussions[http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx]
Exception handling guidelines [http://blogs.msdn.com/ericgu/archive/2006/02/15/532615.aspx]
Exception Throwing [http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx]