Link to home
Start Free TrialLog in
Avatar of kyl011199
kyl011199

asked on

Global exception handler,

 I am trying to write a program that does not use forms Unit So the application object also does not exist.

I am using bare API functions RegisterWindowsClass, CreateWindowsEx and my own message loop. I am also using some units that are written by others.

My Question is, how to cacth and handle exceptions that may be thrown anywhere(from my own units or from the others' units) So how can I write a global exception handler like Application.OnException handler.

Thanx,
Avatar of Lischke
Lischke

Just a quick shot. Wouldn't it be an acceptable solution to wrap the DispatchMessage call into a try except block?

  if PeekMessage(...) then
  begin
    :
    try
      DispatchMessage(Msg);
    except
      on EZeroDivide do HandleZeroDivide;
      etc.
    end;
  end;

Ciao, Mike
Use this one:

procedure ExceptHandler(ExceptObject: TObject; ExceptAddr: Pointer);
begin
  // here you have the exception...
end;

procedure InitExceptions;
begin
  ExceptProc := @ExceptHandler;
end;

Regards, Madshi.
Listening
Avatar of kyl011199

ASKER



Both of you are right.

Madshi: I tried the exceptProc with the following code.


 Procedure ExceptHandler(...);
Begin
  ShowMessage('error');
end;

procedure RaiseException;
Begin
  Raise Exception.Create('TEST');
End;

....

Begin
  exceptProc:= @exceptHandler;
  RaiseException;
End;


the thrown exception was caught by ExceptHandler but after exceptHandler returned Delphi displayed another error message (as if it called ShowException procedure)

  RunTime Error at XXXX address..

How can I get rid of this message..

Thanx you both,
Hmmm... The reason is that Delphi doesn't expect that your program continues running after such an exception occured. If you want to catch the exception and continue with your program you have to use Raymond's suggestion. Otherwise you should call "Halt" or "ExitProcess" in the last line of your ExcptHandler procedure. (Look at the source of SysUtils.ExceptHandler, there they call "Halt" in the last line).

Regards, Madshi.
Madshi send your comment as answer
Lischke I will submit a dummy question for your getting the points.

Thnx you both..
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial