【发布时间】:2014-06-12 16:53:27
【问题描述】:
我正在从包含 statusCode 和 statusMessage 的服务器解析 JSON...我如何将这些扔到我的异常中以便我不必使用 if -我的捕获中的陈述?这样我就可以有一个通用的进程来处理所有exc.Code 和exc.Message,而无需寻找它。
这是我的投球
else if (statusCode.Equals(26) && statusMessage.StartsWith("response sent", StringComparison.OrdinalIgnoreCase))
throw new Exception("Response sent - 26");
else if (statusCode.Equals(0))
throw new Exception("Fatal exception - 0");
else if (statusCode.Equals(3))
throw new Exception("Invalid parameters - 3");
else if (statusCode.Equals(24))
throw new Exception("Incorrect response Id - 24");
这是我的收获
try
{
dataResponse = GetStatus.RequestStatus(httpRequest);
}
catch (Exception exc)
{
if (exc.Message.ToString() == "Response sent - 26")
{
string errorCode = "26";
string errorMessage = "Response Sent";
// do things with erroCode and errorMessage...
}
else if (exc.Message.ToString() == "Fatal exception - 0")
{
string errorCode = "0";
string errorMessage = "Fatal exception";
//do things with errorCode and errorMessage...
}
// else ifs else ifs etc.. etc...
}
finally
{
// do things
}
【问题讨论】:
标签: c# exception-handling try-catch throw