End end

The (quite obvious) output is: ¬°Message: Another: Hello

To make exceptions a little more flexible in case of database-related operations, since the early days of the BDE Delphi introduced the idea of a DBError exception with a list of internal error codes, but this is not very flexible. Now in Delphi 2009, to increase .NET compatibility and to improve exception handling in the dbExpress framework108, there is now system-wide support for nested exceptions.

Within an exception handler, you can create and raise a new exception and still keep the current exception object alive, connecting it to the new exception. To accomplish this, the Exception class has a new InnerException property, referring to the previous exception, and a BaseException property that let's you access the first exception of a series, as exception nesting can recursive. These are the new elements of the Exception class related to the management of nested exceptions, plus a couple of other new methods like the destructor and ToString109:

type

Exception = class(TObject) private

FInnerException: Exception; FAcquirelnnerException: Boolean; protected procedure SetlnnerException; publ ic destructor Destroy; override; function GetBaseException: Exception; virtual; function ToString: string; override; property BaseException: Exception read GetBaseException; property InnerException: Exception read FInnerException; class procedure RaiseOuterException(

E: Exception); static; class procedure ThrowOuterException( E: Exception); static;

From the perspective of a user, to raise an exception preserving the existing one you should call the RaiseOuterException class method (or the identical ThrowOuterException, which uses C++-oriented naming). When you handle a similar exception you can use the new properties to access fur

108 The reason I connect this new feature to the dbExpress framework is simple: the DBXCommon and the DBXPlatform units are currently the only two units of the VCL (for Win32) referring to the InnerException property.

109 I've omitted from this listing of the Exception class other new features compared to Delphi 2007, like the new class data function pointers you can use to hook into exception stack tracing (an advanced topic I won't cover in the book) and the new RaisingException method detailed in the next section.

ther information. Notice that you can call RaiseOuterException only within an exception handler as the source code-based documentation tells:

Use this function to raise an exception instance from within an exception handler and you want to "acquire" the active exception and chain it to the new exception and preserve the context. This will cause the FInnerExceptionfield to get set with the exception in currently in play.

You should only call this procedure from within an except block where this new exception is expected to be handled elsewhere.

As an actual example you can refer to the ExceptionsTest project. In this project I've added a method that raises a nested exception in the new way (compared to the ClassicReraise method listed earlier):

procedure TFormExceptions.MethodWithNestedException; begi n try raise Exception.Create ('Hello'); except

Exception.RaiseOuterException ( Exception.Create ('Another'));

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment