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,
kyl011199Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
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
0
MadshiCommented:
Use this one:

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

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

Regards, Madshi.
0
rwilson032697Commented:
Listening
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

kyl011199Author Commented:


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,
0
MadshiCommented:
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.
0
kyl011199Author Commented:
Madshi send your comment as answer
Lischke I will submit a dummy question for your getting the points.

Thnx you both..
0
MadshiCommented:
Thank you...   :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.