In a follow-up to a previous question regarding exceptions, what are best practices for creating a custom exception in .NET?
More specifically should you inherit from System.Exception, System.ApplicationException or some other base exception?
In a follow-up to a previous question regarding exceptions, what are best practices for creating a custom exception in .NET?
More specifically should you inherit from System.Exception, System.ApplicationException or some other base exception?
Inherit from System.Exception
. System.ApplicationException
is useless and the design guidelines say "Do not throw or derive from System.ApplicationException
."
See http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx
ApplicationException
might actually be a good thing if there were a consistent distinction in the hierarchy between exceptions which mean "The operation couldn't succeed, but the attempt didn't do anything; if you're prepared for that, great", versus "Something has gone so horribly wrong that almost everything should be presumed corrupt; the only reasons not to shut things down instantly would be if one wants to capture diagnostic information or trigger an automatic relaunch, first." Too bad there's no such distinction. –
Disappearance ArgumentException
vs. ArgumentNullException
,ArgumentOutOfRangeException
, etc., but whether that's a critical thing or can be re-tried, or ignored, is still completely up to the application that uses that library. –
Batiste In the C# IDE, type 'exception' and hit TAB. This will expand to get you started in writing a new exception type. There are comments withs links to some discussion of exception practices.
Personally, I'm a big fan of creating lots of small classes, at that extends to exception types. For example, in writing the Foo class, I can choose between:
throw new Exception("Bar happened in Foo");
throw new FooException("Bar happened");
throw new FooBarException();
where
class FooException : Exception
{
public FooException(string message) ...
}
and
class FooBarException : FooException
{
public FooBarException()
: base ("Bar happened")
{
}
}
I prefer the 3rd option, because I see it as being an OO solution.
Inherit from System.Exception
. System.ApplicationException
is useless and the design guidelines say "Do not throw or derive from System.ApplicationException
."
See http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx
ApplicationException
might actually be a good thing if there were a consistent distinction in the hierarchy between exceptions which mean "The operation couldn't succeed, but the attempt didn't do anything; if you're prepared for that, great", versus "Something has gone so horribly wrong that almost everything should be presumed corrupt; the only reasons not to shut things down instantly would be if one wants to capture diagnostic information or trigger an automatic relaunch, first." Too bad there's no such distinction. –
Disappearance ArgumentException
vs. ArgumentNullException
,ArgumentOutOfRangeException
, etc., but whether that's a critical thing or can be re-tried, or ignored, is still completely up to the application that uses that library. –
Batiste There is a code snippet for it. Use that. Plus, check your code analysis afterwards; the snippet leaves out one of the constructors you should implement.
I think the single most important thing to remember when dealing with exceptions at any level (making custom, throwing, catching) is that exceptions are only for exceptional conditions.
The base exception from where all other exceptions inherit from is System.Exception, and that is what you should inherit, unless of course you have a use for things like, say, default messages of a more specific exception.
© 2022 - 2024 — McMap. All rights reserved.