Home All Groups Group Topic Archive Search About
Author
13 Jan 2006 6:35 PM
Brian Henry
When a user has a file open and of course .NET cant access it due to it
being locked i get an error back like this through an exception

System.IO.IOException: The process cannot access the file 'S:\Care.xls'
because it is being used by another process.

is there any way to trap that specific error and give a user friendly
message back? but on any other errors throw an exception? thanks!

Author
13 Jan 2006 6:48 PM
Armin Zingler
"Brian Henry" <nospam@nospam.com> schrieb
> When a user has a file open and of course .NET cant access it due to
> it being locked i get an error back like this through an exception
>
> System.IO.IOException: The process cannot access the file
> 'S:\Care.xls' because it is being used by another process.
>
> is there any way to trap that specific error and give a user
> friendly message back? but on any other errors throw an exception?
> thanks!


try
    'code
catch ex as System.IO.IOException
    'user friendly message
end try


Armin
Author
13 Jan 2006 7:19 PM
Brian Henry
That traps all IO errors though... I just want something when the file is
locked to say something like "You already have this file open, please close
it and try agian" before you could do this with error codes, with exceptions
you dont seem to have them anymore that I know of

I still want IO errors like file not found and stuff to throw exceptions
outside of the one i already mentioned
Author
13 Jan 2006 8:10 PM
Kerry Moorman
Brian,

You could examine the exception's Message property. But that runs the risk
that the message could change in future versions of the framework.

Kerry Moorman


Show quoteHide quote
"Brian Henry" wrote:

> That traps all IO errors though... I just want something when the file is
> locked to say something like "You already have this file open, please close
> it and try agian" before you could do this with error codes, with exceptions
> you dont seem to have them anymore that I know of
>
> I still want IO errors like file not found and stuff to throw exceptions
> outside of the one i already mentioned
>
>
>
Author
13 Jan 2006 8:38 PM
Armin Zingler
"Brian Henry" <nospam@nospam.com> schrieb
> That traps all IO errors though... I just want something when the
> file is locked to say something like "You already have this file
> open, please close it and try agian" before you could do this with
> error codes, with exceptions you dont seem to have them anymore that
> I know of
>
> I still want IO errors like file not found and stuff to throw
> exceptions outside of the one i already mentioned

You wrote that you want to catch only IOException, not all other exceptions.
That's what the example does.

You could rethrow the exception if it's one of the exceptions derived from
IOException: (untested)

try
    'code
catch ex as ioexception
    if ex.gettype is gettype(ioexception) then
        'message here
    else
        throw
    end if
end try


Another way:
const ERROR_SHARING_VIOLATION as integer = 32

try
    'code
Catch ex As IOException When
System.Runtime.InteropServices.Marshal.GetLastWin32Error =
ERROR_SHARING_VIOLATION
    'message
end try


However, I'm not sure whether error #32 is the only error that indicates a
sharing violation.


Armin