Chapter 9: Some Exceptional Exceptions

In This Chapter

check.png Handling errors via return codes

check.png Using the exception mechanism instead of return codes

check.png Plotting your exception-handling strategy

We know it’s difficult to accept, but occasionally a method doesn’t do what it’s supposed to do. (If you need to learn about methods, read the first chapter of Book II). Even the ones we write — especially the ones we write — don’t always do what they’re supposed to. Users are notoriously unreliable as well. No sooner do you ask for an int than a user inputs a double. Sometimes, the method goes merrily along, blissfully ignorant that it is spewing out garbage. However, good programmers write their methods to anticipate problems and report them as they occur.

remember.eps I’m talking about runtime errors, not compile-time errors, which C# spits out when you try to build your program. Runtime errors occur when the program is running, not at compile time.

The C# exception mechanism is a means for reporting these errors in a way that the calling method can best understand and use to handle the problem. This mechanism ...

Get C# 5.0 All-in-One For Dummies now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.