尝试赶上最后质疑 [英] try catch finally question

查看:155
本文介绍了尝试赶上最后质疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个尝试捕捉Finally块,并finally块总是执行不管是什么,或者只在catch块不返回一个错误?

In a Try Catch Finally block, does the finally block always execute no matter what, or only if the catch block does not return an error?

我是IM pression该finally块只有在catch块内未执行的错误之下。如果catch块,是因为一个错误的执行,应该不是停止执行一起,返回错误信息如果有的话?

I was under the impression that the finally block only executes if the catch block passes without errors. If the catch block is executed because of an error, shouldn't it stop execution all together and return the error message if any?

推荐答案

不仅将finally块之后,都执行一个catch块,甚至尽量不要求任何异常被捕获的最终执行。以下是完全合法的code:

Not only will a finally block execute following a catch block, try does not even require that any exception be caught for the finally to execute. The following is perfectly legal code:

try 
{
//do stuff
}
finally 
{
   //clean up
}

我居然拿出catch块在一些code我继承的时候catch块组成的:

I actually took out the catch blocks in some code I inherited when the catch block consisted of:

catch(Exception ex)
{
   throw ex;
}

在这种情况下,所有需要的是要清理,所以我把它只是一个尝试{}最后{}块,并让泡沫例外与他们的堆栈跟踪完整。

In that case, all that was required was to clean up, so I left it with just a try{} and finally{} block and let exceptions bubble up with their stack trace intact.

这篇关于尝试赶上最后质疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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