Connecting Tech Pros Worldwide Forums | Help | Site Map

Proper way to handle UnauthorizedAccessException

cbmeeks
Guest
 
Posts: n/a
#1: Apr 16 '07
I have a project that gathers a list of directories on a computer.
When it hits hidden, protected directories (like c:\system volume
information) it throws an UnauthorizedAccessException error.

As a quick fix, I've captured that error in a try/catch block and just
ignored it (including ignoring the directory).

I don't like kludging like this. Is there a better way?

Thanks

-cbmeeks


Marc Gravell
Guest
 
Posts: n/a
#2: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


That doesn't sound like a kludge to me. You are catching a specific
error for a known, expected reason, and reacting accordingly. Sounds
fine IMO.

Marc


cbmeeks
Guest
 
Posts: n/a
#3: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


On Apr 16, 10:26 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Quote:
That doesn't sound like a kludge to me. You are catching a specific
error for a known, expected reason, and reacting accordingly. Sounds
fine IMO.
>
Marc
Thanks.

I really didn't see any other way but I wanted to make sure.

Thanks again.

-cbmeeks

=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
Guest
 
Posts: n/a
#4: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


If there's nothing you can do, I would agree with Marc: you're handling the
exception.

Depending on the acces violation you could inform the user and ask for
another username/password; but then you'd have to deal with impersonation,
etc.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"cbmeeks" wrote:
Quote:
On Apr 16, 10:26 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Quote:
That doesn't sound like a kludge to me. You are catching a specific
error for a known, expected reason, and reacting accordingly. Sounds
fine IMO.

Marc
>
Thanks.
>
I really didn't see any other way but I wanted to make sure.
>
Thanks again.
>
-cbmeeks
>
>
Peter Duniho
Guest
 
Posts: n/a
#5: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


On Mon, 16 Apr 2007 07:32:58 -0700, cbmeeks <cbmeeks@gmail.comwrote:
Quote:
On Apr 16, 10:26 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Quote:
>That doesn't sound like a kludge to me. You are catching a specific
>error for a known, expected reason, and reacting accordingly. Sounds
>fine IMO.
>>
>Marc
>
Thanks.
>
I really didn't see any other way but I wanted to make sure.
I agree with Marc. IHMO, the reason is *seems* kludgy is probably that
you're used to checking for error return codes and taking appropriate
action based on that. I mean, I don't know what your experience is, but
that's how it is for me. In the "olden days", the only time I ever dealt
with code that used any form of exception handling (setjmp/dojmp), the
exceptions were VERY exceptional. Basically for fatal failures only. We
still used return codes to indicate less-problematic failures, ones that
were benign or could be recovered from.

But .NET uses exceptions for practically any failure. An operation is
assumed to have succeeded, and if it doesn't it throws an exception. This
is just the normal way things work in .NET, and checking for a specific
exception and handling it gracefully is perfectly fine and appropriate.
(As Marc already said, I know :) ).

Anyway, that's not really all that pertinent...just sharing my own
difficulties in getting used to the .NET paradigm, in case they relate to
your own concerns in any way.

Pete
cbmeeks
Guest
 
Posts: n/a
#6: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


On Apr 16, 1:10 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Quote:
On Mon, 16 Apr 2007 07:32:58 -0700, cbmeeks <cbme...@gmail.comwrote:
Quote:
On Apr 16, 10:26 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Quote:
That doesn't sound like a kludge to me. You are catching a specific
error for a known, expected reason, and reacting accordingly. Sounds
fine IMO.
>
Quote:
Quote:
Marc
>
Quote:
Thanks.
>
Quote:
I really didn't see any other way but I wanted to make sure.
>
I agree with Marc. IHMO, the reason is *seems* kludgy is probably that
you're used to checking for error return codes and taking appropriate
action based on that. I mean, I don't know what your experience is, but
that's how it is for me. In the "olden days", the only time I ever dealt
with code that used any form of exception handling (setjmp/dojmp), the
exceptions were VERY exceptional. Basically for fatal failures only. We
still used return codes to indicate less-problematic failures, ones that
were benign or could be recovered from.
>
But .NET uses exceptions for practically any failure. An operation is
assumed to have succeeded, and if it doesn't it throws an exception. This
is just the normal way things work in .NET, and checking for a specific
exception and handling it gracefully is perfectly fine and appropriate.
(As Marc already said, I know :) ).
>
Anyway, that's not really all that pertinent...just sharing my own
difficulties in getting used to the .NET paradigm, in case they relate to
your own concerns in any way.
>
Pete
Thanks for the reply guys.

Now, I have to figure out how to read the entire contents (folders) of
the C:\ drive without it taking 30 seconds on a large drive...lol

cbmeeks

Marc Gravell
Guest
 
Posts: n/a
#7: Apr 16 '07

re: Proper way to handle UnauthorizedAccessException


Now, I have to figure out how to read the entire contents (folders) of
Quote:
the C:\ drive without it taking 30 seconds on a large drive...lol
How about "fdisk" ;-p

Marc

Peter Duniho
Guest
 
Posts: n/a
#8: Apr 17 '07

re: Proper way to handle UnauthorizedAccessException


On Mon, 16 Apr 2007 12:12:28 -0700, cbmeeks <cbmeeks@gmail.comwrote:
Quote:
[...]
Now, I have to figure out how to read the entire contents (folders) of
the C:\ drive without it taking 30 seconds on a large drive...lol
You can do that in only 30 seconds? Geez...I wish I had a computer that
fast. Or with so few directories. :)

Here's a solution for you: do it twice. The second time should be much
faster. :)
Closed Thread