Solved

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

Posted on 2004-10-18
8
438 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

13 Experts available now in Live!

Get 1:1 Help Now