Handling exceptions
You've seen several scenarios where errors have occurred when converting types. Some languages return error codes when something goes wrong. .NET uses exceptions that are richer and designed only for failure reporting. When this happens, we say a runtime exception has been thrown.
Other systems might use return values that could have multiple uses. For example, if the return value is a positive number, it might represent the count of rows in a table, or if the return value is a negative number, it might represent some error code.
Some third-party libraries make it easier to define "result" types that can indicate errors as well as successes. Many .NET developers prefer to use them instead of throwing exceptions. You can learn more about this in an optional online section at the end of this chapter.
When an exception is thrown, the thread is suspended, and if the calling code has defined a try-catch
statement, then it is given a chance to handle the exception...