PDA

View Full Version : Question on Exceptions in Delphi


Darksoul71
31st October 2002, 11:50
Hi there,

No this thread has nothing to do with video related programming :D

this question goes out to the more experienced Delphi programers out there.

Iīm currently writting a text analysys tool for my dimploma at university.
Iīve been using the following IO error checking for nearly all my programs:

//---------------------------
....
//Disable IOChecking
{$I-}
// Do an IO operation
ReadLn ()
//Enable IOChecking
{$I+}

// Check for errors
If (IOResult=0) Then
Begin
// Do some stuff :)
End
// stop on error
else break;
...
//---------------------------

This works fine for simple program where you only open up a single text file. Now I have to
open, close, read, write, etc tons of text files with sizes up to 600 MB. So debugging this is a real pain in the a$$.

A friend of mine suggested to use try...except...else...end


Here a small snippet of my code:
//------------------------------------------------------------------------------
Procedure ErrorLog(Fehler : Integer;Datei : String; Prozedur : String);
Var L : TextFile;
Begin
If (Fehler <> 0) And (Form1.CheckBox2.Checked) Then
Try
AssignFile(L,Form1.Edit13.Text);
If FileExists(Form1.Edit13.Text) Then Reset(L)
Else Rewrite(L);
Except
// Exceptionhandling
Else
Try
// Go on if no error occured
WriteLn(L,'---------------------------------');
WriteLn(L,'Zeit: '+TimeToStr(Time));
WriteLn(L,'Prozedur: '+Prozedur);
WriteLn(L,'Datei: '+Datei);
WriteLn(L,'IO Fehler: '+IntToStr(Fehler));
Except
// Exceptionhandling
Else
CloseFile(L);
End;
End;
End;
//------------------------------------------------------------------------------
Although this worked fine previously with the {$I-} stuff it just drops
back to the end of the file although the error log file should be ok.

I donīt get this.

Has anyone of you experiences with except...end etc. ?
If so, how do I check of IO errors ? How can I get the IO error if an exception occured ?
I would be very happy if some1 could post some sample code for safely accessing textfiles with this try..except stuff. May be with some error handling ?

TIA,
D$

Swede
31st October 2002, 12:02
The syntax is try ... except ... end; not else.

Except :) for:
---
If the exception block specifies an else clause, the else clause handles any exceptions that aren’t handled by the block’s exception handlers. For example,

try

...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
HandleAllOthers;
end;

---

Darksoul71
1st November 2002, 08:50
Hi Swede !

Thank you very much for lightening up my darkness :)

I canīt believe this. Most people are able to read EXCEPT me :D


Iīm too stupid to read the Delphi docs :(

But one question still remains.....

How do I find out what exceptions occur ?
The docs only refer to exception object that start with an E in the name (like EMathError) and that those objects are discribed in the VCL help, but I didnīt find there anything on exception objekts.

Can I find out if an IOError occured with a variable like this "If EIOError.error=103 Then..." ?

TIA once again,
D$

Swede
1st November 2002, 12:26
Hehe.. From the Helpfile:

---
EInOutError is the exception class for file input/output errors.
Unit
sysutils

Description
EInOutError is raised when an operating-system file input/output error occurs, provided the $I+ directive is in effect. The resulting error code is returned in the local ErrorCode variable, which can take the following values.
---
One example: (Typing of my head but it should be correct...)

try
// Some stupid errormaking code..
except
on e: EInOutError do
ShowMessage('You got an IOerror '+IntToStr(e.ErrorCode));
end;

Darksoul71
1st November 2002, 20:02
Hi Swede,

thanks again for helping me out. You donīt know how much you helped me.
This {$I-} stuff made me crazy and the try...except...end works like a charm. :D

Also your sample worked :)

So basicly the way I wanted to use try.... is like this

Fehler is integer and e is EInOutError
....
Fehler := 0;
Try
ReadLn(S,Temp2);
Except
Fehler := e.ErrorCode;
End;

If (Fehler = 0) Then
Begin
// go on if no error occured.
.....
Still one question remains: Why does your version of Delphi (German 5.0 standard) doesnīt show a valid entry if I search for EInOutError ? Neither in the standard help, nor in the VCL help. Strange....

May be I should try to get a new version of Delphi.

Kind regards,
D$