Serge
Serge

Reputation: 199

displaying custom error message rather than system error message in Delphi

I would like my custom error message to show up rather than the one generated by the system. Specifically, in this case, if the user tries to open a file that is already in use, I would like to catch the error and display my message, but the system always beats me to it and generates the following message: "Project...raised exception class EFOpenError with message 'Cannot open file "File path and name". The process cannot access the file because it is being used by another process'." When I close this error message, that is when my message is displayed. I would like only my message to be displayed. I do not want the system message displayed. Is there a way to do this? My code does not work.

Here is my code:

begin
 //check if input file can be opened
      try
        if OpenDialog1.Execute then
        begin
          Application.ProcessMessages;
          Memo1.Clear;
          try
            Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
            InputFile := TStringList.Create;
            InputFile.LoadFromFile(OpenDialog1.FileName);
            ActiveFileName := ExtractFileName(OpenDialog1.FileName);
            lblActiveFileName.Caption := 'Active File: ' + ActiveFileName;
            mmuDisplayClear.Enabled := True;
            mmuAnalysisOptions.Enabled := True;
          except on
            EFOpenError do //if file does not exist or is in use
            MessageDlgPos('File either does not exist or may be ' +
                 'in use!', mtError, [mbOK], 0, 300, 300);
          end;
        end;
      finally
        mmuDispInstructions.Enabled := True;
        mmuDispData.Enabled := False;
      end;
end;

Upvotes: 2

Views: 3217

Answers (2)

SilverWarior
SilverWarior

Reputation: 8331

When you are running an application through Delphi IDE (debuging it) and an exceptions is raised from within try..except..end clause the exception will first be detected by IDE and if you then press F9 code from the except block will fire.

So if you generate custom message there that message will be shown. But you don't need to show a message. You might wanna handle that exception silently.

Let's take a look at the next example: You are trying to establish a network connection to some server. In the call for establishing connection you specify certain timeout after which atempt to connect is considered as failed. After this timeout expires most network components raise a Etimeout exception so you know that establishing connection wa unsucsessfull. But due to the way how networks sometimes behave you might wanna go and retry the atempt to connect to the server again before showing an error to the user. You can do this by simply calling connect method again within the except block in order to initiate another atempt wihout rasing any errors. So code would look someting like this:

Timeout := 2000;
try
  NetworkComponent.Connect(Timout);
except
  try
    NetworkComponent.Connect(Timeout);
  except
    MessageDlgPos('Connection could not be established!, 
                   mtError, [mbOK], 0, 300, 300);
  end;
end;

As you see in the example in first try..except..end block we handle the exception silently but in the next try..except..end block which is nested within the except block of the first try..except..end; block where we finally show message to the user.

While this is generaly considered as bad practice sometimes you might not even want to show any message abot such error. For instance if you are making some system service or a background process one thing that you definitly woul not want is to bother user with bunch of error messages. it is still good to athleast write the errors in some log for further debuging.

Now if you application is ran outside the Delphi IDE (not debuging) or if you turn of specific Exception detection in IDE then the code within the except block will fire directly. when specific exception is raised.

EDIT: Removed unrelated text.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596246

You are seeing the debugger catch the exception before your application does. That is perfectly normal behavior. Simply press F9 to pass the exception to your app for normal handling. If you don't want the debugger to display the exception, put the exception class type in the debugger's Ignore list, or wrap the code in breakpoints that disable/enable the debugger's exception handling.

Upvotes: 5

Related Questions