Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

[Complex] How to raise and catch an exception...

And if you answer "use raise exception.create('Blabla'); then you won't deserve any points! :-P

Okay, situation. I have a very simple application written in Delphi 7 of about 25 KB in size. But the single method it contains might cause an error and I want to raise an exception that I can capture in the except-part of a try-except block.
Now, if I include the SysUtils unit then the problem is easily solved. But hey, guess what? I DON'T want to use the SysUtils unit. It adds almost 20 KB of additional binary data to my executable which is about 20 KB too much in my opinion. I don't want to explain why my simple toll suddenly doubles in size. So how to raise and catch exceptions WITHOUT using any unit except for the Windows unit.

So, NO OTHER UNITS! Raw API stuff. :-)
0
Wim ten Brink
Asked:
Wim ten Brink
  • 4
  • 2
  • 2
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:

You can define a simple exception class, then use the raise at syntax to handle this:

{$DEFINE SIMPLE_EXCEPTION} // For conditional compliation so you can test code size

{$IFDEF SIMPLE_EXCEPTION}
// Scaled down exception object
type
  Exception      =  class(TObject)
  end;
  EError         =  class(Exception);

procedure Error;
  function ReturnAddr: Pointer;
  asm
     MOV     EAX, [EBP - 4]
  end;
begin

  // Raise the simple exception
  raise EError.Create at ReturnAddr;

end;
{$ENDIF}


Then in you code

try
 ...
 if ErrorCondition then Error;
 ...
except
 // catch and handle the error condition
end;


Hope this helps,
Russell




0
 
Lee_NoverCommented:
Windows.RaiseException: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/raiseexception.asp

a basic example is this method:

procedure NameCurrentThread(const AName: string);
{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := PChar(AName);
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;


you can raise any exception .. pass in any data you like and handle it later

RaiseException( $406D1388, 0, sizeof(TObject), MyObject);
don't know how delphi passes exception objects .. would have to check
0
 
Lee_NoverCommented:
doh .. raise "any object" at addr;
ah well .. :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Wim ten BrinkAuthor Commented:
Nice, Russell!

type
  Exception = class(TObject);

begin
  try
    raise Exception.Create;
  except
    on E: Exception do begin
      WriteLn(E.ClassName);
    end;
  end;
  ReadLn;
end.

This is already doing the trick... :-)
Why didn't Borland tell us that you could use ANY object in your raise clause?

@Lee_Nover, nice piece of code but Russell provided a lot easier solution. It's quite useful, in my opinion.
0
 
Lee_NoverCommented:
yep .. agreed :)
I guess 'raise' call's RaiseException in the background and handles it
btw .. your last example .. exactly the same thing I did when trying raise :D .. I mean EXACTLY :D
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Exactly right... the raise statement maps to a little piece of assembly code called  _RaiseExcept (in system.pas), which sets up the actual call to RaiseException.

Regarding Borland; It would have been nice if they took the time to FULLY document the exception handling mechanism in Delphi, instead of forcing the developer to disect it in order to use it to the fullest.

Russell

0
 
Wim ten BrinkAuthor Commented:
IT is nice though that the exception handler mechanism works this way. Otherwise I might have to do a lot of complex things to get it working. But it's the lack of documentation that you won't even find in the Delphi books that are sold all-over this world that is really annoying. If we were beginning or intermediate developers then the manual and other Delphi books would still be useful, but at expert level it's hard to find good solutions for even these simple things. Because in the end, the solution is real simple... :-)
0
 
Lee_NoverCommented:
yep :)
besides raise will always have the same effect regardless of the platform (win32, linux, .net, ...) :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now