Solved

redirecting exception to a log file

Posted on 1997-07-16
3
145 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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