Introduction
The C# language includes exception handling features that help you manage runtime errors that occur due to unexpected or exceptional situations. Exception handling is implemented in C# using the try
, catch
, and finally
keywords. These keywords enable your code to try actions that may not succeed, to handle failures when you decide that it's reasonable to do so, and to clean up resources afterward. Exceptions can be generated by the common language runtime (CLR), by .NET or third-party libraries, or by application code.
Suppose you're developing a purchase and inventory management application using C#. The application processes live purchases and manages the associated inventory data using a combination of user input and embedded business logic. Your initial testing shows that irregularities in purchase order data can produce unexpected error conditions. The errors often result in lost or mismanaged data. You need to implement exception handling and ensure that your application performs in an expected manner before it can be released.
In this module, you learn about exceptions, the exception handling process, and the exception handling patterns supported by C#. You also learn how to implement exception handling for various coding scenarios. Throughout the module, you'll complete challenge activities that reinforce what you've learned.
By the end of this module, you'll be able to develop C# applications that manage unexpected runtime issues and deliver a more stable user experience.
Learning objectives
In this module, you will:
- Examine the basic categories of exceptions and review some common system exceptions.
- Examine the exception handling tools included with C# and the techniques for these tools.
- Implement the
try-catch
pattern within various console application scenarios.
Prerequisites
- An installation of Visual Studio Code that's configured for C# application development.
- Ability to develop C# console applications that implement business logic using iteration statements, selection statements, and custom methods.
- A basic understanding of the relationship between errors and exceptions in C#.
- Experience using the Visual Studio Code debugging tools for C#.