首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#中异常中的异常处理

C#中异常中的异常处理
EN

Stack Overflow用户
提问于 2010-05-18 17:13:27
回答 8查看 1.3K关注 0票数 5

我知道这可能有点奇怪,但怀疑终究是怀疑……在以下情况下会发生什么.

代码语言:javascript
运行
复制
private void SendMail()
{
    try
    {
        //i try to send a mail and it throws an exception
    }
    catch(Exception ex)
    {
        //so i will handle that exception over here
        //and since an exception occurred while sending a mail
        //i will log an event with the eventlog

        //All i want to know is what if an exception occurs here
        //while writing the error log, how should i handle it??
    }
}

谢谢。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-05-18 17:20:48

我会亲自用另一个try\catch语句包装写入事件日志的调用。

但是,这最终取决于您的规范是什么。如果将失败写入事件日志对系统来说很重要,那么您应该允许抛出它。但是,根据您的示例,我怀疑这是您想要做的事情。

票数 4
EN

Stack Overflow用户

发布于 2010-05-18 17:41:39

您可以简单地在错误日志记录方法中捕获错误。然而,我个人不会这么做,因为错误日志记录是你的应用程序根本无法工作的标志。

代码语言:javascript
运行
复制
private void SendMail()
{
    try
    {
        //i try to send a mail and it throws an exception
    }
    catch(Exception ex)
    {
        WriteToLog();
    }
}

private void WriteToLog()
{
    try
    {
        // Write to the Log
    }
    catch(Exception ex)
    {
        // Error Will Robinson
        // You should probably make this error catching specialized instead of pokeman error handling
    }
}
票数 4
EN

Stack Overflow用户

发布于 2010-05-18 17:28:54

只有在try-catch块中才能捕获每个异常。您可以嵌套try-catch,但通常不是一个好主意。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2855836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档