#882 – What Types of Exceptions to Throw
July 8, 2013 5 Comments
When you throw an exception from your code, you should throw one of the following exception types:
- One of the preexisting exception types, if the error matches the exception type (e.g. System.InvalidOperationException or System.ArgumentException)
- System.ApplicationException
- A custom exception type derived from System.Exception
You should not throw exceptions whose type is System.Exception, System.SystemException, System.NullReferenceException or System.IndexOutOfRangeException. These exceptions are thrown by code in the .NET Framework, but you should never throw these exceptions from your own code.
Hi Sean
That’s the reason not to throw these exceptions yourself?
Cheers
Michel
Michel, these exceptions are meant to be thrown by the .NET Framework or the CLR.
Pingback: Dew Drop – July 8, 2013 (#1,580) | Alvin Ashcraft's Morning Dew
Pingback: #890 – Common System Exception Types that You Might Throw | 2,000 Things You Should Know About C#
Pingback: #891 – Example of Throwing a System Exception Type | 2,000 Things You Should Know About C#