Problem with file IO #29

Open
opened 2021-01-19 13:11:53 +00:00 by targetoid · 1 comment
targetoid commented 2021-01-19 13:11:53 +00:00 (Migrated from github.com)

I have a problem with file operations, specifically when there is an exception while writing to a file (when the file does not exist in this instance). When I call Reset(F); on a non existing file, the program just crashes with no message. This only happens after I execute some JavaScript code.

I noticed that in BESENShell, you are calling the Reset function like this {$i-}reset(f,1);{$i+};. Why is it necessary to supress thrown exceptions like this? Are there any other situations, where I can run into this error?

Here is some code that results in a crash. Thanks for your help.

program BESENTest;

{$APPTYPE CONSOLE}

uses
  SysUtils, BESEN, BESENValue, BESENTypes, BESENASTNodes, BESENNativeObject, BESENNumberUtils;

var
  lResult : TBESENValue;
  lResultStr: String;
  lBesen : TBESEN;
  F: file of char;

  function BesenValToStr(aBesenVal : TBESENValue):string;
  begin

    case aBesenVal.ValueType of
      bvtUNDEFINED:     Result := '';
      bvtNULL:          Result := '<null>';
      bvtBOOLEAN:       Result := BoolToStr(aBesenVal.Bool);
      bvtNUMBER:        Result := FloatToStr(aBesenVal.Num);
      bvtSTRING:        Result := aBesenVal.Str;
      bvtOBJECT:        Result := '<object>';
      bvtREFERENCE:     Result := '<reference>';
      bvtLOCAL:         Result := '<local>';
      bvtNONE:          Result := '<none>';
    end;
  end;

begin
  try
    lBesen := TBESEN.Create();
    try
      lResult := lBesen.Execute('1+1');
      lResultStr := BesenValToStr(lResult);
      Writeln(lResultStr);
    finally
      lBesen.Free;
    end;

    AssignFile(F,'doesnotexist.log');
    try
      try
        Reset(F);
        //{$i-}Reset(f);{$i+};
      finally
        CloseFile(F);
      end;
    except
      on Ex: Exception do
        Writeln(Ex.Classname, ': ', Ex.Message);
    end;

    ReadLn;

  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.
I have a problem with file operations, specifically when there is an exception while writing to a file (when the file does not exist in this instance). When I call `Reset(F);` on a non existing file, the program just crashes with no message. This only happens after I execute some JavaScript code. I noticed that in BESENShell, you are calling the Reset function like this `{$i-}reset(f,1);{$i+};`. Why is it necessary to supress thrown exceptions like this? Are there any other situations, where I can run into this error? Here is some code that results in a crash. Thanks for your help. ``` program BESENTest; {$APPTYPE CONSOLE} uses SysUtils, BESEN, BESENValue, BESENTypes, BESENASTNodes, BESENNativeObject, BESENNumberUtils; var lResult : TBESENValue; lResultStr: String; lBesen : TBESEN; F: file of char; function BesenValToStr(aBesenVal : TBESENValue):string; begin case aBesenVal.ValueType of bvtUNDEFINED: Result := ''; bvtNULL: Result := '<null>'; bvtBOOLEAN: Result := BoolToStr(aBesenVal.Bool); bvtNUMBER: Result := FloatToStr(aBesenVal.Num); bvtSTRING: Result := aBesenVal.Str; bvtOBJECT: Result := '<object>'; bvtREFERENCE: Result := '<reference>'; bvtLOCAL: Result := '<local>'; bvtNONE: Result := '<none>'; end; end; begin try lBesen := TBESEN.Create(); try lResult := lBesen.Execute('1+1'); lResultStr := BesenValToStr(lResult); Writeln(lResultStr); finally lBesen.Free; end; AssignFile(F,'doesnotexist.log'); try try Reset(F); //{$i-}Reset(f);{$i+}; finally CloseFile(F); end; except on Ex: Exception do Writeln(Ex.Classname, ': ', Ex.Message); end; ReadLn; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end. ```
JernejL commented 2025-03-07 05:40:11 +00:00 (Migrated from github.com)

The problem could be with https://github.com/BeRo1985/besen/issues/24? - it overrides error handler and does not handle it?

The problem could be with https://github.com/BeRo1985/besen/issues/24? - it overrides error handler and does not handle it?
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
BeRo1985/besen#29
No description provided.