Author Topic: Getting Detailed error for TFileStream  (Read 285 times)

jamie

  • Hero Member
  • *****
  • Posts: 8071
Getting Detailed error for TFileStream
« on: August 29, 2026, 06:45:25 pm »
How do I query the details of TFileStream errors, especially during the CREATE?

EfOpenError isn't enought.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Getting Detailed error for TFileStream
« Reply #1 on: August 29, 2026, 07:20:20 pm »
First, what kind of errors do you expect/predict? I would first list those in the except part of your try/except with a fall-back to Exception for any unsolved case. The usual way, on e:Exxx, On E:YYY, etc.
Any "programmer" that knows only one programming language is not a programmer

Bart

  • Hero Member
  • *****
  • Posts: 5800
    • Bart en Mariska's Webstek
Re: Getting Detailed error for TFileStream
« Reply #2 on: August 29, 2026, 07:54:34 pm »
Query GetLastOSError?

Bart

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Getting Detailed error for TFileStream
« Reply #3 on: August 29, 2026, 08:42:42 pm »
The problem I see is, if I use a FmOpenWrite, did it fail because it does not exist, did it fail because its currently opened elsewhere and locked and so on.

Others like, seeking past the end of file or failed to write.

I like using the TFileStream because it works well for most things, but when it fails via an exception, there is no direct method I can use that is universal that tells me why while inside an Except, at least not that I have seen in the help file, yet!

 The Pascal IO has the IOresults which gives you lots of info when something fails.

Using the system's IO results would be directed to the target and not a cross platform meaning.

maybe I missed something, but I find at times I have to revert to pascal IO to get what's needed but, in my case, I am passing around IO handlers between functions and using streams works perfectly for that.

 What would be nice is an overloaded TFileStream that can populate a IOResults in the Create.

 TFileStream.Create('FileName', Mode, Out IoResult=0);





The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Getting Detailed error for TFileStream
« Reply #4 on: August 29, 2026, 10:05:35 pm »
To clearify what I meant with the enhancement.

Code: Pascal  [Select][+][-]
  1.  
  2. TFilestream.Create(FileName, Mode, Out/Pointer IoResult);
  3.  

What that does is during the use of the stream that variable can be updated at any point, creation or streaming.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Getting Detailed error for TFileStream
« Reply #5 on: August 30, 2026, 07:58:29 am »
So retrieving the error code? A better way is to surface the specific exception, e.g:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils, classes;
  3.  
  4. var
  5.   Stream:TFilestream;
  6.   s:string;
  7. begin
  8.   try
  9.     // one of the rare cases that the creation
  10.     // should be inside the try block or two try blocks
  11.     // A.I. obviously doesn't know that exception to the rule ;)
  12.     Stream := TFileStream.Create ('non-extising-yet',fmOpenRead);// exception
  13.     s:= Stream.ReadAnsiString;  // dummy
  14.   except    
  15.     // shortcut to retrieve any specialized exception class
  16.     On E: Exception do
  17.       writeln(E.Classname,' ',E.Message);// will write the specialized exception name
  18.   end;
  19.   Stream.free;
  20. end.
Prints on my - in the case Dutch - machine:
Code: [Select]
EFOpenError Unable to open file "non-extising-yet": Het systeem kan het opgegeven bestand niet vindenYou can subsequently handle the specialized exception based on classname. There is one exception: EInOutError has an error code conform Appendix D in the Users Guide.
« Last Edit: August 30, 2026, 08:32:43 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

creaothceann

  • Sr. Member
  • ****
  • Posts: 481
Re: Getting Detailed error for TFileStream
« Reply #6 on: August 30, 2026, 08:44:16 am »
The problem I see is, if I use a FmOpenWrite, did it fail because it does not exist, did it fail because its currently opened elsewhere and locked and so on.

Others like, seeking past the end of file or failed to write.
I think it's fine to just show a "could not access file <file name>" message. A finding like "file does not exist" could have a completely different underlying cause, e.g. having no network connection.

Also, these are different types of errors: files not existing, or being locked, or failing to be written are everyday occurences that the program should handle (via the message). Seeking past the end of the file is a serious bug in the program itself, and should end the program with an exception to prevent further damage.


I like using the TFileStream because it works well for most things
SysUtils' FileCreate, FileOpen, FileRead etc. work too.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Getting Detailed error for TFileStream
« Reply #7 on: August 30, 2026, 09:27:45 am »
SysUtils' FileCreate, FileOpen, FileRead etc. work too.
Yes, but you forgot that unlike exceptions or Assign/rewrite/reset, there is no clear error message:
Fileopen just returns THandle(-1) if it fails and no specific error info about the cause, unless you monitor GetLastOSError like you would do with IOResult.
In this case it would be better to use system.assign, system.rewrite/reset in {$I-} state and examine IOResult or use exceptions the way I proposed. Both approaches  are way more informative. And that was the question.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$I-}
  2. var
  3.   err:word;
  4.   f:text;
  5.   fn:string = 'file_does_not_exist';
  6. begin
  7.   system.assign(f,fn);
  8.   reset(f);
  9.   err:=IOResult;// appendix D, Users Guide for error mapping
  10.   writeln(err);
  11. end.
When using exceptions the exceptions are mostly mapped more specific. Not the silly -1.
For this to work correctly you must call GetLastOSError:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$I+}
  2. uses sysutils;
  3. var
  4.   err:word;
  5.   f:THandle;
  6.   fn:string = 'file_does_not_exist';
  7. begin
  8.   try
  9.     f:= FileOpen(fn,fmOpenRead);
  10.   except
  11.     // never throws....
  12.     on e:exception do writeln(e.message);
  13.   end;
  14.   // but the real error is set.
  15.   writeln(GetLastOSError);// appendix D, Users Guide for error mapping
  16. end.

For convenience attached two units that map error codes to string for IOResult and partially GetLastOSError..
One string based, one record based. the second is recommended.
« Last Edit: August 30, 2026, 11:13:00 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018