Skip to content

Instantly share code, notes, and snippets.

@anelson
Created August 31, 2010 10:45
Show Gist options
  • Save anelson/558859 to your computer and use it in GitHub Desktop.
Save anelson/558859 to your computer and use it in GitHub Desktop.
Wrapping exceptions with InnerException, Kyiv 2010
public class UserManager {
public void Authenticate(string userName, string password) {
try {
_userDatabase.Authenticate(userName, password);
} catch (Exception e) {
// Now if authentication fails, when you catch AuthenticationFailedException
// you have no idea what actually caused the authentication to fail.
// That will make debugging, especially in the field, very hard
throw new AuthenticationFailedException();
}
}
}
public class UserManager {
public void Authenticate(string userName, string password) {
try {
_userDatabase.Authenticate(userName, password);
} catch (Exception e) {
// This way, if you log the thrown exception, or look at it in the debugger, you will also see the inner exception
// and its call stack, which will be much more helpful.
throw new AuthenticationFailedException(e);
}
}
}
public class AuthenticationFailedException : ApplicationException {
public AuthenticationFailedException() : base ("Authentication failed") {}
public AuthenticationFailedException(Exception innerException) : base("Authentication failed", innerException) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment