need to implement a global error handling, so maybe you can help out with the following example...
i have this code:
public bool IsUserAuthorizedToSignIn(string userEMailAddress, string userPassword)
{
// get MD5 hash for use in the LINQ query
string passwordSaltedHash = this.PasswordSaltedHash(userEMailAddress, userPassword);
// check for email / password / validity
using (UserManagementDataContext context = new UserManagementDataContext())
{
var users = from u in context.Users
where u.UserEMailAdresses.Any(e => e.EMailAddress == userEMailAddress)
&& u.UserPasswords.Any(p => p.PasswordSaltedHash == passwordSaltedHash)
&& u.IsActive == true
select u;
// true if user found
return (users.Count() == 1) ? true : false;
}
}
and the md5 as well:
private string PasswordSaltedHash(string userEMailAddress, string userPassword)
{
MD5 hasher = MD5.Create();
byte[] data = hasher.ComputeHash(Encoding.Default.GetBytes(userPassword + userEMailAddress));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
stringBuilder.Append(data[i].ToString("x2"));
}
Trace.WriteLine(String.Empty);
Trace.WriteLine("hash: " + stringBuilder.ToString());
return stringBuilder.ToString();
}
so, how would i go about handling exceptions from these functions? they first one is called from the Default.aspx page. the second one is only called from other functions from the class library.
what is the best practice?
try-catchtry-catchwhat to do if exceptions happen?
in this example: this is a user sign in, so somehow even if everything fails, the user should get some meaningful info - along the lines: sign in ok (just redirect), sign in not ok (wrong user name / password), sign in not possible due to internal problems, sorry (exception happened).
for the first function i am worried if there is a problem with database access. not sure if there is anything that needs to be handled in the second one.
thnx for the info. how would you do it? need specific info on this (easier for me to understand), but also general info on how to handle other tasks/functions.
i looked around the internet but everyone has different things to say, so unsure what to do... will go with either most votes here, or most logicaly explained answer :) thank you.
Two golden rules:
Remember that an exception indicates that something has gone wrong, and that particular something may not be what you think. (Out of memory, stack overflow, 3rd party service gone down, botched deployment resulting in missing assemblies, mis-configuration, security exceptions etc).
With very few exceptions, the only place you should see Pokemon exception handling is at the topmost level of your code, where the exception should be published somewhere. For example, in the Application_Error method in your global.asax file.
Here are some links for you that you may find helpful:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With