Solved

redirecting exception to a log file

Posted on 1997-07-16
3
120 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now