3 - Structured Exception Handling
3 - Structured Exception Handling
C#
STRUCTURED EXCEPTION HANDLING
Outline
• In this lesson, we will learn
• How to handle runtime anomalies in your C# code through
the use of structured exception handling
• The various keywords involved in the process
• The distinction between application-level and system-
level exceptions
• The role of the System.Exception base class.
Basic definitions
• There are three commonly used anomaly-centric terms:
• Bugs: errors made by the programmer.
• User errors: caused by the individual running your
application
• Exceptions: runtime anomalies that are difficult, if not
impossible, to account for while programming your
application.
Basics
• The .NET structured exception handling is a technique
for dealing with runtime exceptions.
• However, even for the bugs and user errors that have
escaped your view, the CLR will often generate a
corresponding exception that identifies the problem at
hand.
Basics
• The .NET base class libraries define numerous exceptions, such as
• FormatException
• IndexOutOfRangeException
• FileNotFoundException
• ArgumentOutOfRangeException and so forth.
• Within the .NET nomenclature, an "exception" accounts for bugs,
bogus user input and runtime errors
The Building Blocks of .NET Exception
Handling
• Programming with structured exception handling involves the use
of four interrelated entities:
• A class type that represents the details of the exception
• A member that throws an instance of the exception class to the caller under the
correct circumstances
• A block of code on the caller’s side that invokes the exception-prone member
• A block of code on the caller’s side that will process (or catch) the exception,
should it occur
The Building Blocks of .NET Exception
Handling
• The C# programming language offers four keywords that allow
you to throw and handle exceptions, these are
• try
• catch
• throw
• finally