try catch finally 块是如何工作的? [英] How does the try catch finally block work?

查看:40
本文介绍了try catch finally 块是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中,try catch finally块是如何工作的?

所以如果有异常,我知道它会跳到catch块,然后再跳到finally块.

但是如果没有错误,catch 块不会运行,但是 finally 块会运行吗?

解决方案

是的,无论是否有异常,finally 块都会运行.

<预>尝试[尝试语句][退出尝试][捕捉[异常[作为类型]][当表达式][捕获语句][退出尝试]][ 抓住 ... ][ 最后[finallyStatements]]-始终运行结束尝试

请参阅:http://msdn.microsoft.com/en-us/library/fk6t46tz%28v=vs.80%29.aspx

In C#, how does a try catch finally block work?

So if there is an exception, I know that it will jump to the catch block and then jump to the finally block.

But what if there is no error, the catch block wont get run, but does the finally block get run then?

解决方案

Yes, the finally block gets run whether there is an exception or not.

Try
    [ tryStatements ]
    [ Exit Try ]
[ Catch [ exception [ As type ] ] [ When expression ]
    [ catchStatements ]
    [ Exit Try ] ]
[ Catch ... ]
[ Finally
    [ finallyStatements ] ] --RUN ALWAYS
End Try

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

这篇关于try catch finally 块是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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