的try / catch /终于 [英] try/catch/finally

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

问题描述



我一直想知道最终的目的是什么。什么是

之间的确切区别


尝试

{

somecode1(); < br $>
}

catch(例外)

{

somecode2();

}

终于

{

somecode3();

}


somecode4();

.....和....

尝试

{

somecode1 ();

}

catch(例外)

{

somecode2();

}


somecode3();

somecode4();

谢谢,

亚历山大


I have always wondered what finally is actually for. What would be the
exact difference between

try
{
somecode1();
}
catch(Exception)
{
somecode2();
}
finally
{
somecode3();
}

somecode4();
..... and ....
try
{
somecode1();
}
catch(Exception)
{
somecode2();
}

somecode3();
somecode4();
Thanks,
Alexander

推荐答案

亚历山大,


即使你在某处设置了一个返回,也会执行finally。


Cor
Alexander,

Even when you set a return somewhere, the finally will be executed.

Cor


>我一直想知道最终到底是什么。什么是
> I have always wondered what finally is actually for. What would be the

之间的确切区别

即使抛出异常,最终也会一直执行。

它不会阻止抛出异常,它只执行最后一部分

并继续例外。


一个非常好的例子就是在那里关闭一个文件。

这样即使发生异常,文件也会一直关闭。


-
http://www.skyscan.be


The finally gets ALWAYS executed, even when an exception is thrown.
And it doesn''t block the thrown exception, it just executes the finally part
and continues with the exception.

A very good example for this would be to close a file in there.
This way the file is always closed even if an exception occurs.

--
http://www.skyscan.be


不同之处在于无论发生什么样的
都会执行finally块(未捕获的异常,在catch-block中抛出异常,因此

on)。如果您的应用程序失败,第一个示例将执行

somecode3()但不会执行somecode4(),因为它永远不会达到这一点。

第二个例子不会执行其中任何一个(如果应用程序失败,当然)。

最后非常适合发布数据库连接,关闭

文件,套接字和其他清理操作。
The difference is that the finally-block will be executed no matter what
happens (uncaught exceptions, exceptions thrown in catch-block and so
on). If your application fails, the first example will execute
somecode3() but not somecode4() as it will never get to that point. The
second example won''t execute any of them (if the app fails, of course).
Finally is ideal for things like releasing database-connections, closing
files, sockets and other clean-up operations.

我一直想知道最终是什么。

尝试
{
somecode1();
}
catch(例外)
{
somecode2();
}
最后
{
somecode3();
}

somecode4();

....和....

尝试
{
somecode1();
}
catch(异常) )
{
somecode2();
}

somecode3();
somecode4();

谢谢,< br>亚历山大

I have always wondered what finally is actually for. What would be the
exact difference between

try
{
somecode1();
}
catch(Exception)
{
somecode2();
}
finally
{
somecode3();
}

somecode4();
.... and ....
try
{
somecode1();
}
catch(Exception)
{
somecode2();
}

somecode3();
somecode4();
Thanks,
Alexander



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

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