Solved

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

Posted on 2004-10-18
8
440 Views
Last Modified: 2010-04-05
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
Comment
Question by:Wim ten Brink
  • 4
  • 2
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 12340060

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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12340154
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12340180
doh .. raise "any object" at addr;
ah well .. :)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 17

Author Comment

by:Wim ten Brink
ID: 12340386
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12340899
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 12341680

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
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 12348785
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12349003
yep :)
besides raise will always have the same effect regardless of the platform (win32, linux, .net, ...) :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question