Who is responsible for checking and handling errors?

Who is responsible for checking and handling errors?

I don't have any of the expensive component libraries like DevExpress or TMS Components, etc., so I can't look at the source to understand how most components handle error handling.

In particular, I want to know if there should be a limit on the number of errors and warnings that component developers should try to capture? Is there a balance between meaningful error checking and just too simplification for developers using your component?


Here is an example of using multiple scenarios:

Please note that this is directly from the source of the components (e.g. for purposes)

procedure TMyComponent.AddFromFile(FileName: string);
begin
  FBitmap.LoadFromFile(FileName);
end;

or

procedure TMyComponent.AddFromFile(FileName: string);
begin
  if FileExists(FileName) then
  begin
    FBitmap.LoadFromFile(FileName);
  end
  else
    raise Exception.Create(FileName + ' does not exist.');
end;

And these last two use the component instance at runtime:

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyComponent1.AddFromFile('D:\Test.bmp');
end;

or

procedure TForm1.FormCreate(Sender: TObject);
begin
  if FileExists('D:\Test.bmp') then
  begin
    MyComponent1.AddFromFile('D:\Test.bmp');
  end
  else
    raise Exception.Create('D:\Test.bmp does not exist.');
end;

, , ? ?

, , โ€‹โ€‹, , , .

.

+4
3

:

, , , ? , ?

, , , , , , . , , :

  • .

  • (, ) , , .

  • ( ) as-is.

API, , , , . , , .

:

type
  EMyComponentAddError = class(Exception)
  private
    FFileName: String;
  begin
    constructor CreateWithFileName(const AFileName: string);
    property FileName: string read FFileName;
  end;

constructor EMyComponentAddError.CreateWithFileName(const AFileName: string);
begin
  inherited CreateFmt('Unable to add file: %s', [AFileName]);
  FFileName := AFileName;
end;

procedure TMyComponent.AddFromFile(FileName: string);
begin
  try
    FBitmap.LoadFromFile(FileName);
  except
    Exception.RaiseOuterException(EMyComponentAddError.CreateWithFileName(FileName));
  end;
end;

, , - , . , , InnerException, , , ..

:

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyComponent1.AddFromFile('D:\Test.bmp');
end;

, MyComponent1.AddFromFile('D:\Test.bmp'); . , :

Unable to add file: D:\Test.bmp

, , . , , ? ? , , ? , ? .

, ( - , , ):

procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    MyComponent1.AddFromFile('D:\Test.bmp');
  except
    on E: EMyComponentAddError do
    begin
     ShowMessage('There was a problem adding a file:'+sLineBreak+E.FileName+sLineBreak+sLineBreak+E.InnerException.Message);
     Sysutils.Abort;
    end;
  end;
end;

:

procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    MyComponent1.AddFromFile('D:\Test.bmp');
  except
    on E: EMyComponentAddError do
    begin
      raise Exception.CreateFmt('There was a problem adding a file:'#10'%s'#10#10'%s', [E.FileName, E.InnerException.Message]);
    end;
  end;
end;

:

There was a problem adding a file:
D:\Test.bmp
The file was not found
+2

procedure TMyComponent.AddFromFile(FileName: string);
begin
  FBitmap.LoadFromFile(FileName);
end;

, . , - . .

, . , , ? , , , duff, ? , , LoadFromFile.

. , , .

, , , , . , , , .

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyComponent1.AddFromFile('D:\Test.bmp');
end;

. , :

, ?

, FormCreate. , FileExists() , . , try/except, .

, , .

, , OnCreate . , , , , .

+1

,

procedure TMyComponent.AddFromFile(FileName: string);
begin
  FBitmap.LoadFromFile(FileName);
end;

,

, . TForm1 Application.MainForm, , , . .

  • ,

    procedure TForm1.Form1Create(Sender:TObject);
    begin
      MyComponent.AddFromFile( 'D:\Test.bmp' );
    end;
    

    procedure TForm1.Form1Create(Sender:TObject);
    begin
      try
        MyComponent.AddFromFile( 'D:\Test.bmp' );
      except
        on E: Exception do
          raise Exception.Create( 'Sorry, I cannot run, because of: ' + E.Message );
      end;
    end;
    
  • , , ,

    procedure TForm1.Form1Create(Sender:TObject);
    var
      LBitmapFiles : TStringList;
      LBitmapIdx : Integer;
      LBitmapLoaded : Boolean;
      LErrorStore : TStringList;
    begin
      LBitmapFiles := nil;
      LErrorStore := nil;
      try
        LBitmapFiles := TStringList.Create;
        LErrorStore := TStringList.Create;
    
        LBitmapFiles.Add( 'D:\Test.bmp' );
        LBitmapFiles.Add( 'D:\Fallback.bmp' );
    
        LBitmapLoaded := False;
        while not LBitmapLoaded and ( LBitmapIdx < LBitmapFiles.Count ) do
          try
            MyComponent.AddFromFile( LBitmapFiles[LBitmapIdx] );
            LBitmapLoaded := True;
          except
            on E: Exception do
            begin
              LErrorStore.Add( LBitmapFiles[LBitmapIdx] + ': ' + E.Message );
              Inc( LBitmapIdx );
            end;
          end;
    
        if not LBitmapLoaded then
          raise Exception.Create( 'Sorry, I cannot run, because of: ' + LErrorStore.Text );
      finally
        LErrorStore.Free;
        LBitmapFiles.Free;
      end;
    end;
    

    Other fallback options are possible, and it also depends on the application (fi sets the dummy bitmap for the component) to make the application work correctly.

  • It doesn't matter if we donโ€™t have an image ... we donโ€™t have an image that cares

    procedure TForm1.Form1Create(Sender:TObject);
    const
      CBitmapFile = 'D:\Test.bmp';
    begin
      // check, if there is a file
      if FileExists( CBitmapFile ) then
        try
          MyComponent.AddFromFile( CBitmapFile );
        except
          on E: Exception do
          begin
            // Maybe log the exception
            SomeLogger.Log( E );
            // Maybe set some extra parameters for the application to know, this has failed
            RunningWithoutBitmap();
          end;
        end
      else
        // Maybe set some extra parameters for the application to know, this has failed
        RunningWithoutBitmap();
    end;
    
+1
source

All Articles