尝试在功能内部或外部捕获块以及错误处理 [英] Try Catch blocks inside or outside of functions and error handing

查看:77
本文介绍了尝试在功能内部或外部捕获块以及错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个比语言更具体的通用编程问题。我已经看到了几个想法来尝试捕捉。

This is more a general purpose programming question than language specific. I've seen several appraoches to try and catches.

一个是你对所需的数据进行任何预处理,使用适当的参数调用函数并将其包装成一个尝试/ catch块。

One is you do whatever preprocessing on data you need, call a function with the appropriate arguments and wrap it into a try/catch block.

另一个是简单地调用函数传递数据,并依赖于函数内的try catch,如果错误返回true / false标志发生。

The other is to simply call a function pass the data and rely on try catches within the function, with the function returning a true/false flag if errors occured.

第三是在函数和内部的try catch的组合。但是,如果函数尝试捕获某些东西,那么它会抛出另外一个例外,这个catch函数外的catch块被捕获。

Third is a combination with a try catch outside the function and inside. However if the functions try catch catches something, it throws out another exception for the try catch block outside the function to catch.

对这些方法的利弊的任何想法用于错误控制还是有接受的标准?我的谷歌忍者技能使我无法找到准确的数据。

Any thoughts on the pros/cons of these methods for error control or if there is an accepted standard? My googling ninja skills have failed me on finding accurate data on this.

推荐答案

一般来说,只有在实际上可以处理。

In general, an exception should only be caught if it can actually be handled.

除了登录之外,没有任何目的捕获异常是没有意义的。例外是应该在顶级中捕获异常,以便可以记录异常。所有其他代码应该允许异常传播到将记录它们的代码。

It makes no sense to catch an exception for no purpose other than to log it. The exception is that exceptions should be caught at the "top level" so that it can be logged. All other code should allow exceptions to propagate to the code that will log them.

这篇关于尝试在功能内部或外部捕获块以及错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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