抛出时 TimeoutException 为 null

TimeoutException is null when thrown

抛出的 TimeoutException 对象怎么可能为 null 并抛出

Object reference not set to an instance of an object.

在下一行中:

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);
    }
}

堆栈跟踪和消息是这样的:

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());

无法抛出/捕获空 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());
}
//...