Should I get custom exceptions from Exception or ApplicationException in .NET?

What is the best thing to create when creating exception classes in a .NET solution: System.Exception from System.Exception or from System.ApplicationException ?

+71
exception
Sep 09 '08 at 19:53
source share
5 answers

According to Jeffrey Richter in Frame Design Guides:

System.ApplicationException is a class that should not be part of the .NET platform.

It was assumed that you made sense that you could potentially โ€œexclude allโ€ application exceptions, but the template did not execute and therefore it does not matter.

+56
Sep 09 '08 at 20:02
source share

Even MSDN now says to ignore ApplicationException:

If you are developing an application that needs to create exceptions, it is recommended that you get custom exceptions from the exception class. Initially, user exceptions were supposed to be in the ApplicationException class; however, in practice it was not found to add significant value. See Exclusion Considerations for more information.

http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx

+21
Jul 06 '09 at 12:12
source share

ApplicationException is considered useless - a strong and critical argument.

+18
Sep 09 '08 at 19:58
source share

The authors of the structure themselves consider ApplicationException to be useless:

http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

with a pleasant observation here:

http://blogs.msdn.com/kcwalina/archive/2006/07/05/657268.aspx

If in doubt, I follow their framework development guide.

http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756

The following discusses the topic of a blog post.

gr

+13
Sep 09 '08 at 19:58
source share

I'm used to:

 private void buttonFoo_Click() { try { foo(); } catch(ApplicationException ex) { Log.UserWarning(ex); MessageVox.Show(ex.Message); } catch(Exception ex) { Log.CodeError(ex); MessageBox.Show("Internal error."); } } 

This allows you to make the difference between:

  • C # system system error that I have to execute.
  • A "normal" user error that does not need to be fixed by me.

I know that it is not recommended to use ApplicationException, but it works fine, because there are very few classes that do not respect the ApplicationException pattern.

+1
Jul 06 '09 at 12:07
source share



All Articles