C# Exceptions and the Exception Stack
A bit about Exceptions
C# has introduced possibly the best tool to use for application error trapping: The exception. An exception is an error condition that is raised whenever your application misbehaves or identifies a problemthat needs to be dealt with right away. This tutorial requires that youhave a basic understanding of C# (or Java) exceptions and that you haveused them a few times in your code.
The purpose of this tutorial is to explainfirstly how you would go about creating your own set of exceptions totrap error conditions that could occur in your code or library, foryour own benefit or for the benefit of another developer using yourlibrary. The second lesson is on how the C# exception stack works, and how you can use it to catch all types of exceptions.
Let’s Begin!
The C# Exception Stack
Everyexception that is defined within the .NET framework derives from aclass called Exception. This class is used to describe a general error,whose specificity is not known. For example, a general exception could be an error that is not related in any way to errors that can occurfrom a library you are using.
Every exception inherits from the base class Exception in order to provide a more specific response to the developer. A SocketException indicated to the developer that something went wrong while he was trying to perform an operation with the Socketclass or one of it’s children. There could be a further child exception called TCPStreamException, derived from SocketException, which describes error conditions relating to the transmission of streamed data across a socket.
The result of this inherited characteristicis that you can set up an exception stack to trap errors, starting fromthe most-specific error possible to the least-specific known error.This means that a developer, when performing TCPStream operations, must trap for possible exceptions in the following order: TCPStreamException-> SocketException -> Exception. This will allow him to trap all possible errors, and act intellengently according to the type of exception encountered.
In my example source code, I have three exceptions. From parent to child:
Exception->VehicleException ->MotorCarException-> FerrariException. This follows the classicobject inheritance example ofObject->Vehicle->MotorCar->Ferrari. Can you see how each exception object has a direct connection to the physical object itraises an error for? Examine the source code in the CheckCar class andyou will see the order that each exception is trapped.
Creating your own Exceptions in C#
Thisbecomes a very useful tool when you start creating libraries of code tobe used in your application. Each library which is built to perform aspecific task, should have it’s own set of exceptions that alert thedeveloper of error conditions that can arise when the library is used.<br /><em>Continues…</em><br /><br /><!–nextpage–>
For example, if you have a library thatparses a string, but cannot accept the character ‘&’. You wouldthen create an exception called BadCharacterException which will bethrown each time the application tries to parse a string containing the’&’ character.
Continues…
Pages: 1 2












No comments yet... Be the first to leave a reply!