Board index » delphi » Exception does not catch sharing violations

Exception does not catch sharing violations

I am working on a program that periodically reads data from a file.
Sometimes the file is locked by another application, and when that
happens I get a dialog box with "SYSTEM ERROR...sharing violation on..."
and retry/cancel buttons, and when I choose cancel the program proceeds
into catching the exception.

I have tried both using try/except, and using {$i-} and checking the IOResult
function.
I have tried both using reset('filename') and memo.lines.readfromfile('filename')
and I have found no way to prevent this dialog box from popping up.
My exception handling mechanism handles all other exceptions that happen if
it is not possible to open the file eg. if the file does not exist

I would appreciate all suggestions on how to solve this

Palmi Simonarson

 

Re:Exception does not catch sharing violations


Have you looked into the API function SetErrorMode?  The following code
has worked for me to suppress sharing violation messages, allowing
"except" to handle the error.  ErrorMode is a variable of type Word;

    ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
    try
      MyFile := FileCreate('c:\myfile.ext');
    except
      SetErrorMode(ErrorMode);
      Exit;   {Or whatever..}
    end;

--
Steve Esser, JCPenney Telemarketing, ses...@jcpenney.com
{Of course this is only me talking, not JCPenney.}

Other Threads