Solved

redirecting exception to a log file

Posted on 1997-07-16
3
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…

734 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