Created
August 31, 2010 10:45
-
-
Save anelson/558859 to your computer and use it in GitHub Desktop.
Wrapping exceptions with InnerException, Kyiv 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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