什么是最后块的使用..? [英] whats the use of finally block..?

查看:68
本文介绍了什么是最后块的使用..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天参加了一次采访,当面试官询问使用finally块的优势时,很困惑,因为最终的代码也将执行。



喜欢

yesterday attended an interview and was puzzled when the interviewer asked whats the advantage of using finally block as the code below the finally will also execute.

like

try
     {
         File.OpenRead(@"c:\notihng.txt");
     }
     catch (Exception)
     {
         //gets in here when file is not found.
         // swallow exception.. yummy..:)
     }
     finally
     {
         //gets in here every time..
         //[my code]
     }
     int x2 = 123; //still executes so whats
     // the use of using finally when [my code] can be written here
     // instead of finally.
 }



然后是在finally块中编写代码的用法......!

但是任何方式得到了工作.. :)但想要答案,想到问面试官,但想到这将是一个不错的地方。


then whats the use of writing the code in finally block...!
but any ways got the job..:) but wanted the answer, thought of asking the interviewer but thought this would be a nice place.

推荐答案

你好,



我想在我之前发帖的每个人都提到了你想知道的一切:)



只需添加,想想最后阻止你想要做的事情,无论在try块内发生什么(例如:抛出异常,发现返回语句,一切顺利等等)


最简单的方法是用一些代码解释它。

Hi there,

I think everyone who posted before me has mentioned everything that you want to know :)

Just adding to that, think of finally block as something you want to do regardless what happens inside a try block (E.g.: Exception thrown, Return statement found, everything went smoothly, etc.)

The easiest is to explain it with some code.
SqlConnection connection = null;
try {

    var connectionStringName = connectionComboBox.Text.Trim();
    if (connectionStringName.Length == 0) {
        MessageBox.Show("Please, select a connection");
        return;
    } else {
        connection = new SqlConnection();
        connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ToString();
        connection.Open();
    }

    // More code
}
catch {
    // Handle your exceptions here
    // You can even have multiple catch blocks to take care of different types of exception
    // Or if you handling exceptions at a higher level, just remove the catch block
}
finally {
    if ((connection != null) && (connection.State == ConnectionState.Open)
        try {
            connection.Close();
        }
        catch {
            // Normally, you do not want to log errors like these, so you just suppress them with an empty block
            // But if you want to handle them, you can always go ahead or just remove the catch block to handle it in a higher level
        }
}



在上面的示例中,有多个实例可能会抛出异常,即使在打开连接时也是如此。无论如何,您希望关闭连接到数据库, IF 在代码块执行后打开连接。即使执行 return 语句,finally块也会执行。



要记住的一件重要事情是, a finally 绝不能抛出异常,除非系统设计明确要求你这样做,这一般被认为是不好的做法。



希望这会有所帮助:)问候


In the above example, there are multiple instances where exceptions might be thrown, even while opening a connection. Regardless of that, you want to close the connection to the database, IF there is a connection open after the code block executes. The finally block will execute, even if the return statement is executed.

One important thing to remember is, a finally block MUST NOT throw an exception unless the system design explicitly wants you to do so, which is considered bad practice in general.

Hope this helps :) Regards


最后块编写的代码将无论try-catch中发生了什么,都执行。



所以如果你打开一个SqlConnection说,然后在你的try块中遇到一个大问题并抛出异常,finally块中的代码将在throw操作之前执行 - 允许你整齐地关闭稀缺资源。
Code written in a finally block will execute regardless of what else happens in the try-catch.

So if you open a SqlConnection say, then in your try block you encounter a major problem and throw an exception, the code in the finally block will be executed before the throw is actioned - allowing you to close the scarce resource tidily.


最后块总是执行(如果是异常或没有异常)



这样做的一个用途是释放昂贵的资源,如果在发布之前发生异常,可能会遗漏它。



我们需要它来执行需要执行的操作,而不管流程中是否有任何意外的异常。
Finally block always executes (in case of exception or no exception)

One use of this is to release costly resouces, which may be left unused in case of exception occurs before releasing it.

we need it to perform actions which needs to be done irrespective of any unexpected exception comes in flow.


这篇关于什么是最后块的使用..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆