?
Solved

redirecting exception to a log file

Posted on 1997-07-16
3
Medium Priority
?
147 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 150 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

764 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