Solved

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

Posted on 2004-10-18
8
439 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 79
how do i create updater to My Activex application? 3 79
Making delphi communicate with a c# service 16 89
creating threads in delphi 1 82
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now