?
Solved

Global exception handler,

Posted on 2000-01-19
7
Medium Priority
?
414 Views
Last Modified: 2012-05-04
 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,
0
Comment
Question by:kyl011199
7 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2370044
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2370275
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2372708
Listening
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 

Author Comment

by:kyl011199
ID: 2373923


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
 
LVL 20

Expert Comment

by:Madshi
ID: 2373981
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
 

Author Comment

by:kyl011199
ID: 2384628
Madshi send your comment as answer
Lischke I will submit a dummy question for your getting the points.

Thnx you both..
0
 
LVL 20

Accepted Solution

by:
Madshi earned 400 total points
ID: 2384741
Thank you...   :-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

599 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