The Delphi Language, Part 2 - Exception Classes
(Page 14 of 14 )
Exceptions are merely special instances of objects. These objects are instantiated when an exception occurs and are destroyed when an exception is handled. The base exception object is .NET's System.Exception class.
One of the more important elements of the Exception object is the Message property, which is a string that provides more information or explanation on the exception. The information provided by Message depends on the type of exception that's raised.
Caution - If you define your own exception object, make sure that you derive it from a known exception object such as Exception or one of its descendants. This is so that generic exception handlers will be able to trap your exception.
When you handle a specific type of exception in an except block, that handler also will catch any exceptions that are descendants of the specified exception. For example, System.ArithmeticException is the ancestor object for a variety of math-related exceptions, such as DivideByZeroException, NotFiniteNumberException, and OverflowException. You can catch any of these exceptions by setting up a handler for the base ArithmeticException class, as shown here:
try
Statements
except
on EMathError do // will catch EMathError or any descendant
HandleException
end;
Any exceptions that you don't explicitly handle in your program eventually will continue to unwind the stack until handled. In a .NET Winform or Webform application, a default exception handler performs some work to display the error to the user. In VCL applications, the default handler will put up a message dialog box informing the user that an exception occurred.
When handling an exception, you sometimes need to access the instance of the exception object in order to retrieve more information on the exception, such as that provided by its Message property. There are two ways to do this: The preferable method is to use an optional identifier with the on SomeException construct. You can also use the ExceptObject() function, but this technique is not recommended and has been deprecated.
You can insert an optional identifier in the on ESomeException portion of an except block and have the identifier map to an instance of the currently raised exception. The syntax for this is to preface the exception type with an identifier and a colon, as follows:
try
Something
except
on E:ESomeException do
ShowMessage(E.Message);
end;
The identifier (E in this case) receives a reference to the currently raised exception. This identifier is always of the same type as the exception it prefaces.
The syntax for raising an exception is similar to the syntax for creating an object instance. To raise a user-defined exception called EBadStuff, for example, you would use this syntax:
raise EBadStuff.Create('Some bad stuff happened.');
Flow of Execution
After an exception is raised, the flow of execution of your program propagates up to the next exception handler until the exception instance is finally handled and destroyed. This process is determined by the call stack and therefore works program-wide (not just within one procedure or unit). Listing 5.7 is a VCL unit that illustrates the flow of execution of a program when an exception is raised. This listing is the main unit of a Delphi application that consists of one form with one button. When the button is clicked, the Button1Click() method calls Proc1(), which calls Proc2(), which in turn calls Proc3(). An exception is raised in Proc3(), and you can witness the flow of execution propagating through each try..finally block until the exception is finally handled inside Button1Click().
Tip - When you run this program from the Delphi IDE, you'll be able to see the flow of execution better if you disable the integrated debugger's handling of exceptions by unchecking Tools, Options, Debugger Options, Borland .NET Debugger, Language Exceptions, Stop on Language Exceptions.
Listing 5.7 Main Unit for the Exception Propagation Project
1: unit Main;
2:
3: interface
4:
5: uses
6: Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7: Dialogs;
8:
9: type
10: TForm1 = class(TForm)
11: Button1: TButton;
12: procedure Button1Click(Sender: TObject);
13: end;
14:
15: var
16: Form1: TForm1;
17:
18: implementation
19:
20: {$R *.nfm}
21:
22: type
23: EBadStuff = class(Exception);
24:
25: procedure Proc3;
26: begin
27: try
28: raise EBadStuff.Create('Up the stack we go!');
29: finally
30: ShowMessage('Exception raised. Proc3 sees the exception');
31: end;
32: end;
33:
34: procedure Proc2;
35: begin
36: try
37: Proc3;
38: finally
39: ShowMessage('Proc2 sees the exception');
40: end;
41: end;
42:
43: procedure Proc1;
44: begin
45: try
46: Proc2;
47: finally
48: ShowMessage('Proc1 sees the exception');
49: end;
50: end;
51:
52: procedure TForm1.Button1Click(Sender: TObject);
53: const
54: ExceptMsg = 'Exception handled in calling procedure. The message is "%s"';
55: begin
56: ShowMessage('This method calls Proc1 which calls Proc2 which calls Proc3');
57: try
58: Proc1;
59: except
60: on E:EBadStuff do
61: ShowMessage(Format(ExceptMsg, [E.Message]));
62: end;
63: end;
64:
65: end
Reraising an Exception
When you need to perform special exception handling for a statement inside an existing try..except block and still need to allow the exception to flow to the block's outer default handler, you can use a technique called reraising the exception. Listing 5.8 demonstrates an example of reraising an exception.
Listing 5.8 Reraising an Exception
1: try // this is outer block
2: { statements }
3: { statements }
4: ( statements }
5: try // this is the special inner block
6: { some statement that may require special handling }
7: except
8: on ESomeException do
9: begin
10: { special handling for the inner block statement }
11: raise; // reraise the exception to the outer block
12: end;
13: end;
14: except
15: // outer block will always perform default handling
16: on ESomeException do Something;
17: end;
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |