0% found this document useful (0 votes)
54 views11 pages

3 - Structured Exception Handling

This document discusses structured exception handling in C# and .NET. It defines exceptions as runtime anomalies that are difficult to account for during programming. It explains the keywords try, catch, throw, and finally that allow throwing and handling exceptions. It also describes the core members of the System.Exception base class and distinguishes between system-level exceptions that derive from System.SystemException and application-level exceptions that can derive from System.Exception or System.ApplicationException.

Uploaded by

Ebisa Dugo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views11 pages

3 - Structured Exception Handling

This document discusses structured exception handling in C# and .NET. It defines exceptions as runtime anomalies that are difficult to account for during programming. It explains the keywords try, catch, throw, and finally that allow throwing and handling exceptions. It also describes the core members of the System.Exception base class and distinguishes between system-level exceptions that derive from System.SystemException and application-level exceptions that can derive from System.Exception or System.ApplicationException.

Uploaded by

Ebisa Dugo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Object-Oriented Programming with

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

•The object that represents the problem at hand is a class


extending System.Exception (or a descendent thereof)
Core Members of System.Exception
Property Meaning
Data This read-only property retrieves a collection of key/value pairs
(represented by an object implementing IDictionary) that provide
additional, programmer-defined information about the exception.
By default, this collection is empty.
HelpLink This property gets or sets a URL to a help file or web site
describing the error in full detail.
InnerException This read-only property can be used to obtain information about
the previous exception(s) that caused the current exception to
occur. The previous exception(s) are recorded by passing them
into the constructor of the most current exception.
Core Members of System.Exception
Property Meaning
Message This read-only property returns the textual description of a given
error. The error message itself is set as a constructor parameter.
Source This property gets or sets the name of the assembly, or the object, that
threw the current exception.
StackTrace This read-only property contains a string that identifies the sequence
of calls that triggered the exception. This property is very useful
during debugging.
TargetSite This read-only property returns a MethodBase object, which
describes numerous details about the method that threw the exception
(invoking ToString() will identify the method by name).
System-Level Exceptions
• The .NET base class libraries define many classes that ultimately
derive from System.Exception.
• Exceptions that are thrown by the .NET platform are called
system exceptions.
• They are generally regarded as non-recoverable, fatal errors.
• System exceptions derive directly from a base class named
System.SystemException
Application-Level Exceptions
• You, the programmer, can your own application-specific
exceptions.
• You could derive your custom exceptions from the
System.Exception type.
• Or you could derive your custom exceptions from the
System.ApplicationException class.

You might also like