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