帮助处理finally块中的异常
我正在尝试使用 try-catch-finally 块捕获异常。我已经在 catch 块中捕获了异常,并使用全局异常变量将其传递给finally 块。这样做,我已经处理了finally块中的异常场景。我知道这听起来很奇怪,但有必要这样做。如果有任何编码标准问题,请告诉我。另外,如果您能提出同样的建议,我将非常感激。
谢谢。
I am trying to catch an exception using a try-catch-finally block. i have caught the exception in the catch block and passed it to the finally block using a global exception variable. Doing so, i have handled the exception scenario in the finally block. I know this sounds wierd, but theres a need to do so. Please let me know if there is any coding standard issue with the same. Also if you can suggest something on the same, i will be really obliged.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
catch
是捕获异常并进行任何必要的异常处理。finally
块用于清理所有剩余打开的资源,例如文件句柄、数据库连接等。此块将大部分时间运行,因此是正确的位置进行清理,但不处理异常本身,这些应该在 catch 块中处理。catch
is to catch the exception and do any necessary exception handling. Thefinally
block is there to clean up any left open resources, like file handles, database connections, etc. This block will most of the time run and therefore is the correct place to do cleanup, but no the handle the exceptions themselves, those should be dealt with in thecatch
block.如果按预期使用,try-catch-finally 模式是一种非常有用且强大的模式。 不建议在finally块中处理异常,并且没有多大(任何?)意义。
尝试重新组织代码以适应模式,而不是相反。
小示例:
此外,请查看 Try...Catch...Finally 语句。
The try-catch-finally pattern is a very useful and powerful pattern, if used as intended. Handling exceptions in the finally block is not recommended and does not make much (any?) sense.
Try to reorganize your code to fit the pattern, not the other way around.
Small example:
Also, have a look at the MSDN documentation for Try...Catch...Finally Statements.