tags:

views:

2399

answers:

5

Hi all,

I'm getting an exception which says "Access Denied" when the users permissions are sufficient, how do I catch an exception and check for "Access Denied" so that I can show the user a friendlier "Sorry Access Denied" message?

Thanks Beginner :-)

+4  A: 

If you are using a try catch block...

try
{
    //error occurs
}
catch (Exception ex)
{
    MessageBox.show(ex.Message);
}

Obviously that is pretty crappy error handling, but it shows that the Exception object contains the error string. You can narrow down the handling of different exceptions by catching different exception types.

Try
{
    //error occurs
}
catch (AccessDeniedException ex)
{
    MessageBox.show(ex.Message);
}
catch (FieldAccessException)
{

}
// etc...
Ed Swangren
+10  A: 

You don't really want to check the string of the message, you want to check the type of the message, which can be easily done by catching only the type(s) of exception you are checking for. The following example will catch two different types of exceptions and do different actions based on what if any error occurs. (Note: the names of the exceptions are made up)

try {
    ...
} catch (SomeKindOfException ex) {
    MessageBox.Show(ex.Message);
} catch (AccessDeniedException ex) {
    //Do something else
}
Ed Marty
In this case, you're probably getting an UnauthorizedAccessException.
Jason Anderson
I was editing my comment as you posted this :)
Ed Swangren
+1 for example showing a chain of catches. I'm surprised how often even experienced devs aren't aware of this, though to their credit, they're often coming from a C background.
Greg D
+1  A: 

Simple:

try
{
    YourCommandWhichResultsInDeniedAccess();
}
catch (AccessDeniedException)
{
    MessageBox.Show('Access Denied');
}

If you don't know the type of the Exception and/or you want to check the Exception message instead, do the following:

try
{
    YourCommandWhichResultsInDeniedAccess();
}
catch (Exception e)
{
   if (e.Message == 'Access Denied')
   {
       MessageBox.Show('Access Denied')
   }
}
DrJokepu
Exceptions usually have more details than just access denied. They also may have different case. I would try something like "if (e.Message.ToLower().Contains("access denied"))" as my condition if not catching a specific "AccessDeniedException".
Dan Herbert
With doing a catch on the base Exception type but only taking action when that if statement is true, there is a big gap for other exceptions to go unnoticed. If you do have to catch Exception, there should be an else statement that either handles all other exceptions or re throws the exception.
ManiacZX
A: 

First off - should look into the permissions issue rather than tackling the exception solely. If "Access Denied" is thrown then there must either be a permissions issues or a lock of some sort.

Anyways, "Message" is a string and you can use the .Contains method on it to check for "Access Denied".

You can't change the "Message" property as it has no setter, but you can handle the exception and display a polite message.

MessageBox.Show("Sorry, Access Denied"); for instance.

Edit: as mentioned above you should check for the type of exception. e.g. AccessDeniedException rather than use something as generic as "Exception".


try
        {
            // code here which throws exception
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("Access Denied"))
            {
                MessageBox.Show("Sorry, Access Denied", "This is a polite error message");
            }
        }
Brian H. Madsen
A: 

I think the safest thing to do here (and surprisingly none of the answers show this) is to

  • Catch as specific an exception type as you can. Really try to avoid catching all exceptions.
  • Test the exception message for string.ToLowerInvariant() containing your target string.
  • re-throw if it's not what you expect!

like so:

try
{
    int result = DoStuff(param);
}
catch (System.IO.IOException ioex)
{
    if (ioex.Message.ToLowerInvariant().Contains("find me"))
    {
        .. do whatever ..
    }
    else
    {
        // no idea what just happened; we gotta crash
        throw;
    }
}
Jeff Atwood