Hi
First of all please forgive me, I seem to be having a bit of a mental block! I've been trying to improve my exception handling and error logging.
I have a function ChangeName(). Within that function I call a ValidateUser() function. And within that function I call the GetAdmin() function.
I have omitted unimportant code, so:
public void ChangeName(...)
{
if(ValidateUser())
//Do stuff
else
//log error
}
public bool ValidateUser(...)
{
if(IsAuthenticated && GetUser(...))
return true;
else return false;
}
public bool GetUser(...)
{
//Access DB and if found
return true;
else
return false;
}
My question is, where should I have the try/catch blocks?
Do I just have it in ChangeName() so that all errors propogate back to this function, or do I have one in each function? I'm trying to get my head around which is best practice. I also realise that I shouldn't just catch Exception, but catch specific types first.
Thanks
Loading
VulpesPosted Sep 14, 2011, 6:11 AM
Vilas GitePosted Sep 14, 2011, 5:11 AM
Hi..
Really valuable explanation by Vulpes.
Refer this link also, may be helpful for you…
http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx
http://www.csharp-station.com/Tutorials/lesson15.aspxThanks!
----------------------------------------------------------------------
If this reply helps your post…then check "This is correct answer"
AdamPosted Sep 14, 2011, 3:53 AM
Class 1:
public void ProcessPerson(Request arg)
{
try
{
if(Class2.ValidateUser(arg.Username, arg.Role)
{
//Do something
}
else
{
//log error
}
}
catch(ArgumentNullException ex)
{
//do something
}
catch(Exception ex)
{
//do something
}
}
Class 2:
public static bool ValidateUser(string Username, string Role)
{
try
{
using(MyClass myClass = new MyClass())
{
if(myClass.GetAdmin(Username, Role)
return true;
else
return false;
}
}
catch(ArgumentNullException ex)
{
throw;
}
catch(Exception ex)
{
throw;
}
}
MyClass (in this class I don't know whether to just let the error happen when the adapter method is called or to test the values beforehand. I think I should probably be testing the values first?:
public bool GetAdmin(string Username, string Role)
{
// should I do this?
if(String.IsNullOrWhitespace(Username) || String.IsNullOrWhitespace(Role))
{
throw new ArgumentNullException("e.g. Username or Role cannot be null");
}
try
{
using(MyClassAdapter myClassAdapter = new MyClassAdapter ())
{
myClassAdapter.GetAdmin(Username, Role);
}
}
catch(ArgumentNullException ex)
{
throw;
}
catch(Exception ex)
{
throw;
}
}
Appreciate any advice or if anyone else wants to comment?
Thanks.
VulpesPosted Sep 13, 2011, 1:38 PM
Sometimes, even though it's caught an exception, the method in which it's thrown is not best placed to take remedial action - perhaps because the exception is a consequence of arguments that were passed to it by the calling method - and in this case it should be rethrown so that an exception handler earlier in the chain can hopefully catch and do something about it.
This is particularly relevant where you're writing class libraries for others and therefore have little control over the arguments that may be passed to a particular method.
VulpesPosted Sep 13, 2011, 12:19 PM