Error Handling in C# with Exceptions

Understanding and using exceptions is crucial to creating readable code that responds correctly to errors at runtime. This course will teach you how to handle errors in C# applications by throwing, catching, filtering, and customizing exceptions.
Course info
Rating
(35)
Level
Intermediate
Updated
Aug 6, 2018
Duration
1h 37m
Table of contents
Course Overview
Understanding the Importance of Error Handling
Understanding the Exception Class Hierarchy
Getting Started with Exceptions
Catching, Throwing, and Rethrowing Exceptions
Creating and Using Custom Exceptions
Writing Automated Tests for Exception Throwing Code
Description
Course info
Rating
(35)
Level
Intermediate
Updated
Aug 6, 2018
Duration
1h 37m
Description

At the core of handling errors in C# code is a thorough knowledge of exception handling. In this course, Error Handling in C# with Exceptions, you’ll learn how to write code that can detect and respond to runtime errors. First, you’ll learn why exceptions are used to represent errors in C# and how they are organized into class hierarchies. Next, you’ll explore how to throw, catch, filter, rethrow, and wrap exceptions. Finally, you’ll discover how to define, throw, and catch your own customized exception classes and also write unit tests for exception throwing code. When you’re finished with this course, you’ll have a thorough knowledge of C# exceptions that will help you to create production-ready C# applications that detect and respond to runtime errors.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Working with Files and Streams in C#
Intermediate
3h 13m
12 Oct 2018
Automated Testing with MSTest V2
Intermediate
2h 2m
18 May 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone, my name's Jason Roberts. Welcome to my course, Error Handling in C# with Exceptions. I'm a freelance developer and author. In this course, you're going to learn what exceptions are, and how to use them to handle errors in your C# applications. Some of the major topics that we'll cover in this course include the exception class hierarchy and useful standard exception types, different ways to throw and handle exceptions, how to create custom exceptions, and how to unit test exception throwing code. By the end of this course, you'll understand how C# exceptions work, and how to use them to manage errors in your C# solutions. Before beginning the course, it's recommended that you have at least a basic understanding of C#. I hope you'll join me on this journey with the Error Handling in C# with Exceptions course at Pluralsight.

Understanding the Importance of Error Handling
Hi everyone. I'm Jason Roberts from Pluralsight. Welcome to this Error Handling in C# with Exceptions course. Errors in production systems can cause a lot of stress, not only to end users, but also to software developers, operations people, and managers. Errors can occur for multiple reasons such as bugs in the code, network problems, or even catastrophic hardware failures. Exception handling in C# allows runtime errors to be managed in a structured way, and can help improve stability and manageability of production applications and systems. In this course, we'll be focusing on C# exception handling in a general way, and you can use this knowledge in. NET Framework apps,. NET Core apps, and other places where C# is used. In this first module, we'll get an understanding of the importance of error handling, and we'll kick off this module by answering the question why handle errors in the first place. We'll then learn about one way errors were handled in the past using error codes, and we'll learn why this is not a good idea in C# applications. Rather than error codes, in C# we make use of exceptions to handle error situations. So we're going to learn why we use exceptions, and we'll finish off this module by getting an overview of what exceptions are.

Understanding the Exception Class Hierarchy
Hi, welcome back. In the previous module, we learned why we want to handle errors, and why exceptions are the preferred error handling approach in C#. In this module, we're going to be digging a little bit deeper, and understanding more about the Exception class hierarchy. So we're going to kick off this module by discussing what an exception represents. We'll learn that exceptions are organized into a class hierarchy, and that the System. Exception class is the base class for all exception types. We'll learn about some commonly used constructors to create exceptions, and we'll also discuss some guidelines for the use, or more appropriately the nonuse of the ApplicationException class. And finally, we'll learn about some commonly encountered exception types. So let's kick off this module by learning what an exception represents.

Getting Started with Exceptions
Hi, welcome back. In the previous module, we learned more about the exception hierarchy, we learned about the System. Exception base class, and some commonly-encountered exceptions, such as the ArgumentNullException and the NullReferenceException. In this module, we're going to start putting some of this knowledge to work. So in this module, we're going to get started by understanding more about exception handling when we're writing C# applications. We're going to learn how exceptions bubble up the call stack, and we're going to get an introduction to the try statement. We'll have a look at the demo code we'll be using for the rest of this course, and then we'll go and cause an exception to be thrown in the initial version of the demo code. We'll see that when an exception is thrown, we can get access to the stack trace, in addition to other information. We'll then learn how to create and throw an exception as part of our own code, and we'll also get started with exception catching. Finally, we'll round out this module with some exception handling good practices. So let's kick off this module by getting an understanding of exception handling and exception bubbling.

Catching, Throwing, and Rethrowing Exceptions
Hi, welcome back. In the previous module, we got started throwing and catching exceptions. In this module, we're going to be digging deeper, and we're going to learn some additional techniques to catch exceptions, throw exceptions, and also rethrow exceptions. So we're going to kick off this module by learning how we can throw exceptions from expressions. We'll learn how we can have multiple catch blocks, each catching a different type of exception. We'll see the finally block in action, and we'll learn the correct and incorrect way to rethrow an exception, and if we don't do this correctly, how lose valuable stack trace information. We'll also learn how we can catch an exception, and then wrap it in a new instance of an exception. We will learn how we can further refine when a catch block is activated by making use of exception filters. And we'll also learn how we can implement global, unhandled exception handling. So let's kick off this module by learning how we can throw exceptions from expressions.

Creating and Using Custom Exceptions
Hi, welcome back. In the previous module, we learned more about catching, throwing, and rethrowing exceptions, but up until this point in the course, we've been working with exception types provided to us by the. NET Framework. In this module, we're going to learn that we can actually create and use our own custom exception types. So we're going to kick off this module by getting an understanding of custom exceptions. So we'll get a high-level overview, we'll learn when it may be appropriate to use custom exceptions, and we'll also get an overview of implementing our own custom exception types. We'll then head into Visual Studio, and we'll define a new custom CalculationException type. And we'll also define a second custom exception type, this time to represent an operation that's not currently supported by the calculator. We'll also learn that we can add properties to custom exceptions, so we'll go ahead and do this, and finally we'll go and modify the program to make use of our custom exceptions. So let's kick off this module by getting a high-level understanding of custom exceptions.

Writing Automated Tests for Exception Throwing Code
Hi, welcome back. In the previous module, we learned how to create and use custom exceptions. In this module, we're going to learn how we can write automated tests for code that throws exceptions. So we're going to kick off this module by learning how to test exceptions with MSTest. We're then going to see how to test exceptions with NUnit. And we're also going to learn how to use xUnit. net to test exception throwing code. Finally, we'll look at some resources and further-learning opportunities. So let's head over to Visual Studio, and we'll learn how we can test that exceptions are thrown at the correct time with MSTest.