If I need to throw an exception from within my application which of the built-in .NET exception classes can I use? Are they all fair game? When should I derive my own?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
See Creating and Throwing Exceptions.
On throwing built-in exceptions, it says:
and
This blog entry also has some useful guidelines.
Also, FxCop code analysis defines a list of ‘do not raise exceptions’ as described here. It recommends:
So in theory you can raise any other framework exception type, providing you clearly understand the intent of the exception as described by Microsoft (see MSDN documentation).
Note, these are ‘guidelines’ and as some others have said, there is debate around System.IndexOutOfRangeException (ie many developers throw this exception).