try catch 块中debug时发现错误细节的一次记录

在解决已有代码的一个问题时,有一个try catch块,基本代码如下:

try
{
    //do something
}
catch
{

    LogHelper.Debug(typeof(myHelper), string.Format("Could not find country code for IP Address {0}", ipAddress));

}

调试时,发现程序执行到try块时,发生错误,直接跳过去执行catch块了,而catch块里面只是调用LogHelper去记日志,还是不知道错误信息到底是什么,到底为什么出错了。

怎么解决呢? 改代码如下

try
{
    //do something
}
catch(Exception ex)
{

    LogHelper.Debug(typeof(myHelper), string.Format("Could not find country code for IP Address {0}", ipAddress));

}

这样,进入catch块时,通过调试就可以看到Exception ex的错误信息

原文地址:https://www.cnblogs.com/wphl-27/p/7802172.html