Is there any way, to auto generate the “catch”, for all possible exception, that a method can throw, in vs 2010 or with third party application?
For example if I use “Directory.CreateDirectory” it will automatically create:
try
{
Directory.CreateDirectory("blabla");
}
catch (PathTooLongException)
{}
catch (DirectoryNotFoundException)
{}
catch (IOException)
{}
catch (ArgumentNullException)
{}
catch (UnauthorizedAccessException)
{}
There is a tool which finds unhandled exceptions in your code: Exception Hunter by Red Gate software.
As it turns out, they discontinued it:
This should be an indication that “catching all exceptions that a method can throw” might be a bad idea. The usual pattern is to have a global catch-all
only at the top level of your user interface (WinForms), or to handle errors in dedicated methods (WPF: Application.DispatcherUnhandledException, WebForms: Application.Error).
You handle an exception directly in code only in the exceptional case that you expect this exception to occur and you know how to deal with this one specifically and how to continue with your program execution afterwards.
As a side note: What you want looks quite similar to a Java feature called “checked exceptions“: It forces you to either handle an exception or declare that your method will re-throw it. The following question explains why the designers of C# deliberately chose not to include this feature: