Which exception handling should I choose for a client connect
I have a client class which connects to a server via WebRequest
. Now I'm struggling with how I could best implement the WebException
handling. For a good UI feedback I only need (so far) the exception message and the status.
I came up with three different approaches. The first simply rethrows the exception and I do the handling in the calling method. This might be the most universal solution:
public class ClientSimpleRethrow
{
public bool IsConnected { get; set; }
public void Connect()
{
try
{
// do connection
}
catch (WebException)
{
IsConnected = false;
throw;
}
IsConnected = true;
}
}
The second version throws a custom exception with the WebException
as inner exception. This gives me the benefit of only catching and handling exactly what I expect. For example, there might be another bug which also throws a WebException
that I would like to treat differently. So far this is my favorite solution:
public class ClientThrowCustom
{
开发者_运维技巧 public bool IsConnected { get; set; }
public void Connect()
{
try
{
// do connection
}
catch (WebException ex)
{
IsConnected = false;
throw new ClientConnectionException(ex.Message, ex);
}
IsConnected = true;
}
}
There would also be a third possibility I can think about. The exception is catched and I set only the properties I really need to handle the exception in the UI. The info that something went wrong I already have via the IsConnected
property. This solution does have the benefit of not catching twice.
public class ClientProperties
{
public bool IsConnected { get; set; }
public string ConnectionErrorMessage { get; set; }
public WebExceptionStatus? ConnectionErrorStatus { get; set; }
public void Connect()
{
try
{
// do connection
}
catch (WebException ex)
{
IsConnected = false;
ConnectionErrorMessage = ex.Message;
ConnectionErrorStatus = ex.Status;
return;
}
IsConnected = true;
ConnectionErrorMessage = null;
ConnectionErrorStatus = null;
}
}
I would need some best practice advice which one I should choose and why. I hope this question is not too subjective because I really think, there should be some hard facts why to choose one design over the others.
Thanks in advance for your help!
For starters, I think the third method is bad. The nice things about exceptions is they don't force the calling context to check error codes before proceeding. Inside the try block you can write your code as if every function is successful. The third method takes away this nicety and makes the calling code look more complicated.
As for the first two, I prefer the second. It has two benefits in my eyes:
You can make an Exception class that means something in the context of your application and not work around the context of an underlying API.
WebException
is good for the API, but if you get it in a method when you are trying to log into some system,LogInException
is more helpful at the upper layers of your application.Custom exceptions allow you to start at a base
MyApplicationException
class and build out from there. From there, you can catch API exceptions and turning them intoMyApplicationException
child exceptions as soon as possible. This way when an API exception ripples up into your application, you know immediately that there is some corner case that you did not account for and needs to be addressed. Where as an uncaught application exception just means you are not using your own code correctly.
精彩评论