try/catch Blocks

To get a real appreciation for C# exception handling, consider the error-handling methods used in procedural programming languages with no built-in exception handling mechanism. For example, look at the following C programming language error-handling routine:

int someMethod();
...
int result;
result = someMethod();
if (result != 0) {
    // do some error handling
}

In this example there's a prototype of someMethod(), showing that it returns an int. Under the prototype is code that would normally be part of a routine. The result variable captures the return value from someMethod(). Then the program checks the return to see whether it's nonzero. If so, there must have been an error, and it is handled right there.

This is the way ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.