Search code examples
.netexceptionrole-base-authorization

SecurityException vs UnauthorizedAccessException


My application has role based security. When an user attempts to perform some operation, and the role he is in does not have that permission, we throw an exception.

void DeleteFile(String fileName) {
  if(!_role.canDeleteFile()) {
    throw new Exception();
  }

  //delete it
}

Now i was refactoring this code and find the usage if base Exception class ugly. Instance of what class should i use? Or should i subclass one?

Edit

I searched in the framework and found two. but none of them actually fits. SecurityException seems to be part of BCL itself. UnauthorizedAccessException is under IO namespace. Which exception is the most appropriate (to inherit from) in this case?


Solution

  • As the UnauthorizedAccessException is under the IO namespace it's typically used for file access etc.

    SecurityException is the base exception in Code Access Security and is probably my first choice.