SimonORorke
asked on
TDriveComboBox exception handling
When an empty CD drive or empty floppy drive is selected in a DriveComboBox, the default behaviour is to show an error message box with the message 'I/O error 21.' When running the program from within Delphi in debugging mode with the 'Break on exception' option, this is shown to be an exception of type EInOutError. I want to replace the default exception handler with my own.
I tried the following DriveComboBox.Change event handler. To test it, I ran the program with 'Break on exception' switched off. The problem is that the exception event occurs as soon as the event handler is entered: debug tracing indicates that control passes to the event handler, but none of the code in it gets executed. And I cannot see what other event handler should include the code instead.
procedure TForm2.DriveComboBox1Chang e(Sender: TObject);
begin
{$I-} // Switch off automatic exception handling
try
OKBitBtn.Enabled := True;
except
OKBitBtn.Enabled := False;
on IOE : EInOutError do begin
if IOE.ErrorCode = 21 then begin
{show my own message}
end else begin
{show the default message}
end;
end;
on E : Exception do begin
{show the default message}
end;
end;
{$I+} // Switch on automatic exception handling
end;
I have tried it with and without the '{$I+}' and '{$I-} directives: I do not know whether they are required. Either way, it does not solve the problem.
I tried the following DriveComboBox.Change event handler. To test it, I ran the program with 'Break on exception' switched off. The problem is that the exception event occurs as soon as the event handler is entered: debug tracing indicates that control passes to the event handler, but none of the code in it gets executed. And I cannot see what other event handler should include the code instead.
procedure TForm2.DriveComboBox1Chang
begin
{$I-} // Switch off automatic exception handling
try
OKBitBtn.Enabled := True;
except
OKBitBtn.Enabled := False;
on IOE : EInOutError do begin
if IOE.ErrorCode = 21 then begin
{show my own message}
end else begin
{show the default message}
end;
end;
on E : Exception do begin
{show the default message}
end;
end;
{$I+} // Switch on automatic exception handling
end;
I have tried it with and without the '{$I+}' and '{$I-} directives: I do not know whether they are required. Either way, it does not solve the problem.
ASKER
Edited text of question
ASKER
Edited text of question
Hi Simon
that can't work. your expection-handler will only be executed
when in OKBitBtn.Enabled := True; an expection occurs.
i'm not so familiar with expection-handling, but i will try
to find a solution.
regards
rene100;
that can't work. your expection-handler will only be executed
when in OKBitBtn.Enabled := True; an expection occurs.
i'm not so familiar with expection-handling, but i will try
to find a solution.
regards
rene100;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello rene100!
In your comment, you suggest that your proposed solution cannot work. But actually it works fine! That is to say, it gave me enough information that I was able to easily work out the fine details myself. For the record, here is the full solution.
procedure TForm1.FormCreate(Sender: TObject);
...
Application.OnException := DoException;
...
procedure TForm1.DoException(Sender: TObject; E: Exception);
begin
if E.Message = 'I/O error 21' then begin
Form2.OKBitBtn.Enabled := False;
MessageDlg(
'Drive ' + UpperCase(Form2.DriveCombo Box1.Drive ) +
' is empty. You will need to select a different ' +
'drive before you can press the OK button.',
mtWarning, [mbOK], 0);
end else begin
Application.ShowException( E);
end;
end;
procedure TForm2.DriveComboBox1Chang e(Sender: TObject);
...
{Required in case an empty drive was previously selected, in which case Form1.DoException will have shown a warning message and disabled the OK button.}
OKBitBtn.Enabled := True;
...
Thanks for your help.
Simon
In your comment, you suggest that your proposed solution cannot work. But actually it works fine! That is to say, it gave me enough information that I was able to easily work out the fine details myself. For the record, here is the full solution.
procedure TForm1.FormCreate(Sender: TObject);
...
Application.OnException := DoException;
...
procedure TForm1.DoException(Sender:
begin
if E.Message = 'I/O error 21' then begin
Form2.OKBitBtn.Enabled := False;
MessageDlg(
'Drive ' + UpperCase(Form2.DriveCombo
' is empty. You will need to select a different ' +
'drive before you can press the OK button.',
mtWarning, [mbOK], 0);
end else begin
Application.ShowException(
end;
end;
procedure TForm2.DriveComboBox1Chang
...
{Required in case an empty drive was previously selected, in which case Form1.DoException will have shown a warning message and disabled the OK button.}
OKBitBtn.Enabled := True;
...
Thanks for your help.
Simon
ASKER