Solved

TDriveComboBox exception handling

Posted on 1998-05-16
6
370 Views
Last Modified: 2010-04-06
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.DriveComboBox1Change(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.
0
Comment
Question by:SimonORorke
  • 4
  • 2
6 Comments
 

Author Comment

by:SimonORorke
ID: 1346011
Edited text of question
0
 

Author Comment

by:SimonORorke
ID: 1346012
Edited text of question
0
 

Author Comment

by:SimonORorke
ID: 1346013
Edited text of question
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 2

Expert Comment

by:rene100
ID: 1346014
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;
0
 
LVL 2

Accepted Solution

by:
rene100 earned 100 total points
ID: 1346015
hi simon

now, her's your solution:
first, create a new procedure with this form:
procedure YourName(Sender: TObject; E: Exception);

then, assign this procedure to the exception-handler with:
(in FormCreate) application.OnException:=YourName;

now, every exception will pass trough YourName, the parameter
Sender is the object that made the exception, E is the exception-
object

hope this helps

rene100



0
 

Author Comment

by:SimonORorke
ID: 1346016
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.DriveComboBox1.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.DriveComboBox1Change(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
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 167
How to convert memory stream to PDF file 6 125
Installshield for Embarcadero EX 10.1 Berlin 4 60
Tidtcpserver listening on multiports? 1 35
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question