异常处理尝试catch块 [英] exception handling try catch block

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

问题描述

您好,



我们使用try catch finally块来处理异常。



try

{}

catch

{}

终于

{}





总是需要执行finally块....如果不是那么请用简短的例子解释

< b>



谢谢&问候

Srishti Gupta

推荐答案

1. finally块应该用于执行必须执行的代码没有异常抛出,比如处理使用过的资源(try块中使用的一次性资源),在其他情况下,finally块不是强制性的。



2.你可以在我的下一篇文章中查看更多详细信息(包括示例源代码): MVC基本站点:第2步 - 例外管理 [ ^ ]
1.The finally block should be used for executing code that is mandatory to be executed even there is no exception thrown, like disposing the used resources (disposable resources used in try block), in other cases the finally block is not mandatory.

2.You could see more details (including example source code) in my next article: MVC Basic Site: Step 2 - Exceptions Management[^]


尝试:您用于执行操作的代码。



Catch :处理在Try块中执行操作时可能发生的异常(用非常简单的单词'错误')。



最后:无论的结果如何,都保证执行 Catch



最后不是强制性阻止,但在例如情况下有用关闭数据库连接等



问候..
Try : Your code which you used to perform operations.

Catch : To handle exceptions(in very simple words 'Errors') that may occur while performing operations inside Try block.

Finally : Its guaranteed to be executed regardless of result of Try or Catch.

Finally is not mandatory block,but its useful in cases e.g. close database connections etc.

Regards..


嗨Srishti Gupta,请查看这篇文章:使用Try ... Catch ...,最后! [ ^ ]



为您提供有关异常处理的良好见解..



永远不必强制使用finally块。但这是可取的。您可以处置所有不需要的对象。当你需要编写一些需要执行的代码而不管是否发生异常时,它也可以使用。



希望它有所帮助。



问候

Dinesh Kumar.V。
Hi Srishti Gupta, Please check this article: Using Try... Catch..., Finally![^]

Gives you a good insight of exception handling..

It is not mandatory to have a finally block always. But it is advisable. You can dispose all the objects which you don't need. Also it can be used when you need to write some code that needs to be executed irrespective of an exception occurs or not.

Hope it helps.

Regards
Dinesh Kumar.V.


这篇关于异常处理尝试catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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