Solved

redirecting exception to a log file

Posted on 1997-07-16
3
135 Views
Last Modified: 2010-04-06
I have all my error messages set up as exceptions. How can i set it up so that any exceptions occuring are written to a log file as well as the screen. (D1).

Thanks.

Tom.
0
Comment
Question by:tomcorcoran
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1338969
Here is a very simple example of an exception logger in D1:

unit Main;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Menus;

type
  TMainForm = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    procedure MyExceptionHandler(Sender: TObject; E: Exception);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.MyExceptionHandler(Sender: TObject; E: Exception);
var
  LogFile: TextFile;
begin
  AssignFile(LogFile, 'log.txt');
  if not FileExists('log.txt')
  then ReWrite(LogFile)
  else Append(LogFile);
  Writeln(LogFile, DateTimeToStr(Now), ': ', E.Message);
  CloseFile(LogFile);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Application.OnException := MyExceptionHandler;
end;

procedure TMainForm.Button1Click(Sender: TObject);
var
  x: Real;
begin
  try
    x := x/0;
  except
  end;
end;

end.

Hope this is what you need!

/// John

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1338970
Hmm-hmmm!
I screwed up! To handle the exceptions this way you have
to re-raise the exception after "except":

  try
    x := x/0;
  except
    raise
  end;

Or simply exclude the try-blocks...!

Sorry!

/// John
0
 

Author Comment

by:tomcorcoran
ID: 1338971
John,

Thanks. Simply a matter of assigning a procedure to the Application.OnException and then you can do as you want.

Cheers.

Tom.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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