使用Final块 [英] Use of Finally Block

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

问题描述

class MainClass
{
        static void Main()
        {
        try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
        }
        Console.writeline("Code executes without Finally");  //code executes without finally
        Console.readline();
    }
}


class MainClass
{
        static void Main()
        {
        try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
        }
        finally {
            Console.writeline("executes inside Finally");  // Code executes with finally
            Console.readline();
        }
    }
}


finally块有什么用?


What is the use of finally block ? The same code in the finally block executes without using finally block.

推荐答案



即使在catch块中有一个例外,finally块的使用也将执行一个代码块.例如.

Hi,

The use of finally block is to execute a block of code even if there is an exeception in catch block. For example.

try
{
   //some code that throw exception and
   //in your code you are using some unmanaged resource like opening DB connection, file etc
}
catch(Exception ex)
{
   //handle exception but while writing log it is throwing exception
   
}
finally
{
   // you should clean unmanaged resouce in finally block because it gaurantees to execute.
}



另一个例子.

如果您在try块中使用return



An other example.

if you are using return in try block

try
{
//some code and then
  return something;
}
catch(Exception ex)
{
}
finally
{
}
//No code will be executed after the finally block if return statement is avail in try block.




看看这个页面:

http://msdn.microsoft.com/en-us/library/fk6t46tz%28v = vs.71%29.aspx
Hi,

have a look at this page :

http://msdn.microsoft.com/en-us/library/fk6t46tz%28v=vs.71%29.aspx


无论什么情况,finally块始终执行.当引发异常或更早地退出函数时,这可能会有所帮助.
请参阅此样本,该样本也最终会调用
The finally block is always executed, no matter what. This may be helpful when throwing exceptions or leaving your function earlier.
See this sample which also calls finally
try {
  //do something
  return;
}
finally {
  Console.writeline("executes inside Finally");  // Code executes with finally
  Console.readline();
}


还是这个:


Or this one:

try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
            throw;
        }
        finally {
            Console.writeline("executes inside Finally");  // Code executes with finally
            Console.readline();
        }


在这两种情况下,都执行了finally块,但是未执行catch处理程序/返回之后的普通"代码.因此,在您的情况下,这可能没有多大意义,但在我的两个示例中,它们是有道理的.
有关更多说明,请参见此处:
http://msdn.microsoft.com/en-us/library/zwc8s4fz% 28VS.80%29.aspx [ ^ ]
希望有帮助!


In both cases the finally block is executed, but "normal" code after the catch handler/the return is not executed. So, in your case it may not make very much sense, but in the two samples from me they make sense.
See here for some more explanations:
http://msdn.microsoft.com/en-us/library/zwc8s4fz%28VS.80%29.aspx[^]
Hope that helps!


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

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