抛出时,TimeoutException为null

时间:2015-02-24 12:52:22

标签: c# .net service-reference timeoutexception

如何抛出TimeoutException对象为null并抛出

  

对象引用未设置为对象的实例。

在以下行中:

  

writeToLog(e2.ToString());

查看此代码。

WebServiceRef.CallResponse callResponse = null;
try
{
    callResponse = webServiceClient.Call(callRequest);
}
catch (TimeoutException e)
{
    try
    {
        WebServiceRef.CallStatusResponse callStatusResponse = webServiceClient.CallStatus(callStatusRequest);
        if (callStatusResponse.ResponseCode != 0)
        {
            throw new Exception("nok: " + callResponse.ResponseCode);
        }
    }
    catch (TimeoutException e2)
    {
        writeToLog(e2.ToString());
    }
}  

这是我的writeToLog方法。

private static void writeToLog(String logMsg)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"log.txt", true))
    {
        file.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss ") + logMsg);
    }
}

Stacktrace和消息是这样的:

Object reference not set to an instance of an object.
 at ...(...) in c:\...cs:line 82
 at ...(...) in c:\...cs:line 193

第82行指向

  

writeToLog(e2.ToString());

1 个答案:

答案 0 :(得分:0)

无法抛出/捕获空Exception实例。

您的调试符号必须出错或者您没有运行正确的程序。尝试在此处记录一些测试字符串以确保执行正确的代码:

//...
catch (TimeoutException e2)
{
    Debug.WriteLine("If you don't see this in the output window then somehow you are not running this app.");
    writeToLog(e2.ToString());
}
//...