【问题标题】:Try-Catch-Finally c# in Console [closed]Try-Catch-Finally c# in Console [关闭]
【发布时间】:2017-09-28 18:25:24
【问题描述】:

我需要写一个 Try-Catch-Finally。 首先,我是编程新手。 回到问题。

在 Try-Block 中,我想打开一个不存在的文本。

在 Catch-Block 中,消息框应显示 FileNotFoundException。

我仍然不知道应该在 finally 块中放什么。

try
{
   FileStream File = new FileStream("beispiel.txt", FileMode.Open);
}
catch (FileNotFoundException fnfex)
{
   //MessageBox with fnfex
}
finally
{
   //idk
}

谢谢

【问题讨论】:

  • 你的问题是什么?
  • 如果你需要一个 finally 块,你只需要一个。听起来你没有理由这样做。
  • 仅供参考,您并不总是需要 finally
  • 您在finally 块中放入的内容取决于您要执行的逻辑。这是什么逻辑?
  • 您可能想要关闭 Stream 或作为更好的选择使用 using 语句。

标签: c# try-catch try-catch-finally


【解决方案1】:

Finally 用于有保证的做某事的方式,即使抛出异常。在您的情况下,您可以例如处理您的流,但通常最好对实现 IDisposable 的对象使用 using 语句,这样您就可以这样做

using (var stream = new FileStream(...))
{
   // do what ever, and the stream gets automatically disposed.
}

见:

using statement

IDisposable

【讨论】:

    【解决方案2】:
    DialogResult result;
    
    try
    {
        FileStream File = new FileStream("beispiel.txt", FileMode.Open);
    }
    catch (FileNotFoundException fnfex)
    {
        result =
            MessageBox.Show(
                this, 
    
                // Message: show the exception message in the MessageBox
                fnfex.Message, 
    
                // Caption
                "FileNotFoundException caught", 
    
                // Buttons
                MessageBoxButtons.OK,
                MessageBoxIcon.Question, 
                MessageBoxDefaultButton.Button1, 
                MessageBoxOptions.RightAlign);
    }
    finally
    {
        // You don't actually NEED a finally block
    
    }
    

    【讨论】:

      【解决方案3】:

      最后是始终执行的代码。我会说使用 finally 块来清理任何可能存在的东西是一种常见的模式。例如,如果您有文件流...

      如果类型不正确,请原谅,我目前没有 C#,但模式仍然存在......

      FileStream file;
      
      try {
          file = new FileStream("example.txt", FileMode.Open);
          file.open();
      }
      catch (Exception ex) {
          //message box here
      }
      finally {
          // Clean up stuff !
          if (file != null) {
              file.close()
          }
      }
      

      【讨论】:

      • 最好使用 using 声明 IMO
      • 我同意@maccettura,但它仍然有效并且确实解决了操作员的要求:)
      【解决方案4】:

      catchfinally 的共同用法是在 try 块中获取和使用资源,在 块中处理异常情况>catch 块,并释放 finally 块中的资源。欲了解更多信息,请查看https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally

      由于您只想捕获异常然后打印出一条消息,因此您可以简单地摆脱 finally 块,如下所示:

      try
      {
         using (FileStream File = new FileStream("beispiel.txt", FileMode.Open)){}
      }
      catch (FileNotFoundException fnfex)
      {
         //MessageBox with fnfex
         MessageBox.Show(fnfex.Message);
      }
      

      using 语句确保对象一旦超出范围就被释放,并且不需要显式代码或 finally 来确保发生。

      【讨论】:

        【解决方案5】:

        除了人们已经说过的 try...catch...finally 块之外,我相信您正在寻找的是

        try {
            file = new FileStream("example.txt", FileMode.Open);
            file.open();
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }
        

        但您需要在项目中添加对 System.Windows.Forms 的引用,并将 using 语句添加到您的类中

        using System.Windows.Forms;
        

        或者,如果你只想在控制台中显示消息,你可以使用

        Console.WriteLine(e.Message);
        

        忘记参考

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-30
          • 2019-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-26
          • 2011-08-27
          • 2014-11-27
          相关资源
          最近更新 更多