Could you please explain to me what the difference is between an error and an exception?
An exception is an object of a type deriving from the System.Exception
class. It is used in a throw
statement to transfer control to a catch
clause in a try
block somewhere further up the call stack.
An error is just some code or message that you're meant to interpret. The problem with error codes is that you can decide to ignore them:
MethodThatReturnsAnError();
SomeCodeThatShouldNotExecuteOnError();
That call will simply ignore the error code if one is returned. However:
MethodThatThrowsAnException();
SomeCodeThatShouldNotExecuteOnError();
This cannot be ignored, and will transfer control up the stack, past "SomeCodeThatShouldNotExecuteOnError();
".
An exception is a class that takes advantage of language semantics. As others have stated, exceptions interrupt execution up the stack until caught. An exception can be used to convey an error, but more generally is used to convey that something exceptional has occurred.
Errors, on the other hand, can be exceptional or not.
There are several kinds of errors:
- User error - this should be handled without an exception
- Syntax error - this shouldn't compile in statically typed languages (in dynamic languages, they're a little harder to discover)
- Runtime error - this will either result in an exception, or silently fail (usually creating unexpected results)
Really, exceptions should be limited to handling runtime errors, since a user inputting bad data is not "exceptional." To handle user errors, you should take the following approaches:
- Prevent bad data from being input (front-end validation)
- Prevent bad data from being persisted (back-end validation)
Exceptions should be used as a "last line of defense" for user error. If you're writing a persistence layer, you can rely on exceptions to ensure that bad data that falls through validation does not get persisted. You should, however, fix any of these by putting a fix in the validation that prevents the error from occurring in the first place.
Exception: When a step in some action fails, all the subsequent steps in that action are simply NOT executed. This is where exceptions shine.
Error: is when, like in the first case you want to halt execution of the current code, but before you do you need to free any resources previously allocated.
Having that said,
Exception class has HResult property. HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code.
Have a look at this post, will help you understand better
Exceptions are a way of reporting and handling execution failures. In other words, they are for communicating error conditions (paraphrasing Krzysztof Cwalina in the book Framework Design Guidelines).
Exceptions you have to write code to ignore. Error codes you have to write code to not ignore.
Usually, I classify them as:
Error - is a known workflow within the application. For example: Username not provided during authentication is an error.
The application can handle these situation & will be able to show friendly messages to the user to prompt for proper input and/or process the data in a different.
Exception - is usually throw when going out of your system and/or something unexpected happens in the application. For example: opening a file handle might throw an exception due to insufficient rights or the file not existing.
Usually in this case, the application can catch these exception and/or write a generic handler to handle all the exceptions in the system.
As a rule of thumb, if you know a particular case exists due to which the application cannot proceed working, label it as an error & handle the case gracefully.
All remaining 'unknown-unknows' can then fall into the category of Exceptions.
HTH.