Ode to Errors, Bugs, and Exceptions
The Role of .NET Exception Handling
The .NET platform provides a standard technique to send and trap runtime errors that is common to all languages targeting the .NET platform: structured exception handling.
The Building Blocks of .NET Exception Handling
The four interrelated entities involve in structured exception handling: a class type that represents 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.
C# has five keywords to throw and handle exceptions: try, catch, throw, finally, when.
The object that represents the problem at hand is a class extending System.Extension (or a descendent thereof).
The System.Exception Base Class
All exceptions ultimately derive from System.Exception.
Article notes
What is the standard technique provided by the .NET platform to send and trap errors, that is common to all languages targeting the platform, called?
Structured exception handling
All exceptions in C# ultimately derive from what class?
System.Exception
What read-only property of System.Exception retrieves a collection of key-value pairs (represented by an object implementing IDictionary) that provide additional, programmer-defined information about the exception?
Data
What read-only property of System.Exception can be used to obtain information about previous exceptions that caused the current exception to occur?
InnerException
When you throw a new System.Exception object in .NET, what read-only property gets set via the class constructor?
Message
What read-only property of System.Exception contains a string that identifies the sequence of calls that triggered the exception?
StackTrace
What is the keyword used in C# to raise an exception?
throw
Example:
throw new Exception($"{PetName} has overheated!");
What can you make use of in C# programming when you are invoking a method that may throw an exception?
A try/catch block
What are exceptions thrown by the .NET platform called?
System exceptions
What is a good .NET class to derive custom exceptions from instead of System.Exception?
ApplicationException
What is the default access modifier applied to a non-nested type in .NET?
internal
What optional block can be used with try/catch in C# to ensure that some code will always execute regardless of an exception happening or not?
finally