xbo*_*nez 1 c# exception-handling
这是我的代码的骨架:
if(CheckForSomething())
{
try
{
//do something
}
catch (UnauthorizedAccessException ex)
{
LogException(ex, item.server);
}
catch (Exception ex)
{
LogException(ex, item.server);
}
}
else
{
string error = "Some error";
//want to call LogException with error as argument
}
private static void LogException(Exception ex)
{
//write ex to one log file
// write ex.message to another log file
}
Run Code Online (Sandbox Code Playgroud)
如何从else块调用LogException方法?我尝试将字符串转换为异常并创建异常.
一个更好的问题imo不是如何,但为什么你想要这样做?为什么不定义两个LogError重载,一个采用a Exception而另一个采用a string.
private static void LogError(Exception ex)
{
// write ex to one log file
// write ex.message to another log file
}
private static void LogError(string error)
{
//nothing to write to exception-specific log file
// write error info to another log file
}
Run Code Online (Sandbox Code Playgroud)
为这样的Exception日志记录生成自己的实例并不可取.
LogException(new Exception("some error"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |