【问题标题】:What code gets executed if an exception is thrown? [closed]如果抛出异常,会执行什么代码? [关闭]
【发布时间】:2011-05-03 06:37:55
【问题描述】:
try
{
    some code
}
catch()
{
    some code
}
finally
{
    some code
}

try
{
    some code
}
catch()
{
    some code
}
finally
{
    some code
}

我知道如果在第一个 try 块中抛出异常,那么第一个 finally 块将被执行。第二个 finally 块呢?

另外,如果您想在出现异常时向用户显示消息,那么您应该在哪里编写该消息,以及您应该如何显示它?

仅供参考,我最近在一次采访中被问到这些问题,被难住了。

【问题讨论】:

  • -1:如果您希望我们花时间回答您的问题,请花时间整理一个连贯的问题。
  • 不,先生,这不是唯一的问题,我的面试官问我在哪里卡住了,我把它放在这里是为了了解你们知识分子背后的概念
  • 我编辑了问题以尽可能多地删除不必要的叙述。人们喜欢你只问问题,而且你的问题的标题尽可能具体,又不过分冗长。
  • 好的先生,以后我会专注于这个
  • 还要考虑如何回答问题。给“一些代码”项目编号会很有帮助。

标签: c# logging exception-handling


【解决方案1】:

在您编写的代码中,因为它们是两个不同的 try 块(即一个不包含在另一个中。)它们都将被尝试,第二个在第一个 finally 块之后运行。第二个 finally 块也将运行。

第二个是视情况而定的,一般来说,您希望让您的异常尽可能远离用户。你希望你的程序成为管家,安静,不碍事,但在你需要的时候就在那里。如果是我,我可能会悄悄记录问题,然后尽可能以最理智的方式继续,除非这是一个大问题并且您需要通知用户,例如“我的 ftp 客户端找不到网络联系。”如果是这种情况,并且您使用的是 C#,那么我建议您查看此页面:http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

【讨论】:

  • 您的意思是说,如果第一个 try 块出现异常,那么仍然是第一个和 finally 块以及与第二个 try catch 块对应的其他 finally 块也会执行..请告诉我... .我也想知道在相应的catch块中向用户显示确切异常的一些消息是否正确,请简要介绍一下...我也在使用记录器LOG4Net,但我想在某些异常上显示适当的消息我在哪里显示它
  • 消息框类适用于windows应用程序,但如何在web应用程序中显示
  • 你的意思是,如果异常出现在第一个 try 块中,那么第一个 try 块将与第二个一样被执行,但是第二个如何对应于第二个 try 块
【解决方案2】:

将此代码复制并粘贴到编辑器中。然后玩弄它,取消注释重新注释不同的行。然后编译并运行代码。继续这样做,直到您对它了解一切感到非常自在。当您发现所有这些仅基于流控制的令人困惑的问题时,我建议您这样做。这就是您将学习编程流控制的方式。

try
{
  Console.WriteLine("try1");
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(ArgumentNullException e1)
{
  Console.WriteLine("catch1");
  Console.WriteLine(e1.ToString());
  // throw;
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(Exception e1a)
{
  Console.WriteLine("catch1a");
  Console.WriteLine(e1a.ToString());
  // throw;
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
finally
{
  Console.WriteLine("finally1");
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}

try
{
  Console.WriteLine("try2");
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(ArgumentNullException e2)
{
  Console.WriteLine("catch2");
  Console.WriteLine(e2.ToString());
  // throw;
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(Exception e2a)
{
  Console.WriteLine("catch2a");
  Console.WriteLine(e2a.ToString());
  // throw;
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}
finally
{
  Console.WriteLine("finally2");
  // throw new ArgumentNullException();
  // Console.WriteLine(((string)null).Length); // Will also throw exception
}

【讨论】:

    【解决方案3】:

    *当我说程序崩溃时,应用程序会死掉并引发运行时异常,因为它没有被捕获,并且没有执行进一步的代码。

    try
    {
        some code1 //always executes this, on exception goto code2
    }
    catch()
    {
        some code2 //if exception was caught do this, if exception occurs in this code program blows up
    }
    finally
    {
        some code3 //always executes this, if exception happens here, program blows up
    }
    
    
    
    
    try
    {
        some code4 //if program has not blown up at this point, execute this. on exception goto code 5
    }
    catch()
    {
        some code5 // if exception was caught do this. if exception occurs in this code, program blows up.
    }
    finally
    {
        some code6 //if program has not blown up by now, always do this.
    }
    

    常见的代码路径是: 1-3-4-6 表示未发现异常 和 1-2-3-4-5-6 如果在 1 和 4 中发生异常 其他情况很少见(您的错误处理已损坏)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多