Sometimes I dont know witch type of exception should I throw. So I usually throw Exception(). Is there some nice article about this?
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.
If you are not making any attempt to recover from an error, you can just throw an
Exceptionwith a string to tell you what went wrong. (Or if you will perform the same action regardless of what error occurred).Other than making it obvious to a programmer what went wrong by using a new exception name rather than putting it in the Message, and exception can contain data to help you recover from a particular exception.
For example, an
ArgumentNullExceptionhas a propertyParamName, which you should set when you throw the exception. When the caller catches it, he can then look up this property and decide to pass a new value for the argument which caused the error, or can print a relevant error to inform the programmer what went wrong.It’s unfortunate that exceptions are rarely used to their full potential (in many open source APIs and such), and are often simply inserted to inform a programmer what went wrong. There’s not much difference between these 2 if you don’t plan to read the
ParamNameproperty when you catch it. (Many people will not bother, and only catch anExceptionanyway).It can be difficult to recover from an error fully, but in some applications though, you might find it desirable to create custom exceptions which can provide all the details you need.
For now, I would just put
Exceptioninto the Object browser search in VS, and see what is there already. Their names are pretty self explanatory, so you should be able to pick out something suitable.