Skip to content

Problem with file IO #29

@targetoid

Description

@targetoid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions