Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-18
8
Medium Priority
?
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 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
Technology Partners: 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!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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