O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required