The try and catch blocks

The exception message provided by the default exception handler, as demonstrated in the sample output from Listing 19.1, is useful for a programmer analyzing his or her source code. However, most end users would be baffled by a program that abruptly terminates and writes out, for them, a cryptic message.

To avoid this kind of behavior and let the program take corrective actions instead without terminating the program, we can enclose the code we want to monitor for special conditions inside a try block. After the try block, we can insert one or more catch blocks that specify the different exception object types we want to handle. I have used this approach on Listing 19.1 to form Listing 19.2, where the division by zero ...

Get C# Primer Plus 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.