简体   繁体   中英

auto generated catch in visual studio 2010

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:

With the release of .NET 4.0 and WPF, the number of exceptions that the CLR can throw was greatly increased, to the point of being overwhelming. The exclusions list can no longer cover all the unlikely exceptions that the CLR may throw. This means that, although Exception Hunter will provide accurate results, these results will include a long list of potential exceptions, most of which are nothing to worry about.

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

try {
    ...
} catch (Exception ex) {
    logAndShowErrorMessage(ex);
}

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:

Silently ignoring the exceptions is the very bad practice, thus, of course, there is no way to auto-generate the catch with an empty body. Also, it is unknown which exceptions the method can throw, since in C# one does not declare which exceptions might a function throw (as opposed to eg Java).

If you still want to ignore all the exceptions, you can just write catch(Exception) , which will catch all the exceptions since all the exceptions are inherited off the base Exception class. But are you sure that silently ignoring everything that may happen is a good idea? It is better to let user (or caller) know that something went wrong instead of silently not doing the expected work (imagine, for example, if Directory.CreateDirectory would act in such a way, so that you never know whether it succeeded in creating the directory or not, and if not, what is the reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM