Exception Handling

Let's start our tour through the world of errors and problems. C# provides simple methods to deal with errors. In this section, we take a closer look at the most important mechanisms.

try/catch

try and catch are the most important keywords you can use for handling errors. The advantage of C#'s way of handling errors is that more than just one problem can be caught at once. In C#, every function needs its own code for handling exceptions properly. It's comparatively hard to check entire blocks for errors. Although code fragments can be packed into functions, the problem is still there.

try and catch make many things significantly easier. Before we discuss the details, let's look at a basic example:

 using System; class Demo ...

Get Mono Kick Start now with O’Reilly online learning.

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