如何有效地使用PHP中的try ... catch块 [英] How to efficiently use try...catch blocks in PHP

查看:159
本文介绍了如何有效地使用PHP中的try ... catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP代码中一直使用try..catch块,但我不知道我是否正确使用它们。

I have been using try..catch blocks in my PHP code, but I'm not sure if I've been using them correctly.

例如,我的一些代码如下所示:

For example, some of my code looks like:

 try {
      $tableAresults = $dbHandler->doSomethingWithTableA();
      $tableBresults = $dbHandler->doSomethingElseWithTableB();
 } catch (Exception $e) {
      return $e;
 }

所以我在同一个try / catch块中分组多个数据库操作,因为如果有任何异常发生在任何一个事务中,我将能够处理它。

So I'm grouping multiple database operations in the same try/catch block because if any exception occurs in any of the transaction, I will be able to handle it.

我这样做是因为我觉得它的可读性和效率比:

I'm doing it that way because I think that it's more readable and efficient than:

 try {
       $tableAresults = $dbHandler->doSomethingWithTableA();
 } catch (Exception $e) {
       return $e;
 }
 try {
       $tableBresults = $dbHandler->doSomethingWithTableB();
 } catch (Exception $e) {
       return $e;
 }

虽然,我不知道我在做什么是一个很好的实践或只是一种懒惰的方式来捕获异常。

Although, I'm not sure if what I'm doing is a good practice or just a lazy way to catch exceptions.

我的假设是,只有当异常需要特殊处理时,它应该有自己的try / catch块,否则将它们分组在同一个try / catch应该可以。

My assumption is that only if an exception requires special handling, it should have its own try/catch block, otherwise grouping them in the same try/catch should be ok.

所以我的问题是:

每个数据库使用try / catch块有什么好处吗交易?或者我还可以在同一个try / catch块中将多个数据库事务分组在一起,没有任何问题?

Is there any advantage of using try/catch blocks per database transaction? or can I still group multiple database transactions in the same try/catch block with no problem at all?

嵌套try / catch块可以吗?
谢谢!

Is it ok to nest try/catch blocks? Thanks!

编辑

返回语句主要用于但是我也使用 catch()中的返回值,因为我正在对该方法发出一个AJAX请求,而Javascript正在期待一个JSON对象,那么如果发生异常,我返回一个空的JSON编码数组。我只是想,在我的例子中,它不会添加任何值来放置特定的代码。

The return statement was primarily for demonstration purposes only, but I'm also using returns in catch() because I'm making an AJAX request to that method, and Javascript is expecting a JSON object, then if an exception occurs I return an empty JSON encoded array. I just thought that It wouldn't add any value to put specific code in my example.

推荐答案

重要说明

以下讨论假定我们正在讨论如上例所示的代码结构:无论选择哪种替代方法,异常会导致逻辑上的方法不要在中间做任何事情。

The following discussion assumes that we are talking about code structured as in the example above: no matter which alternative is chosen, an exception will cause the method to logically stop doing whatever it was in the middle of.

只要你打算做同样的事情,无论哪个语句在 try 块抛出异常,那么使用一个 try / 。例如:

As long as you intend to do the same thing no matter which statement in the try block throws an exception, then it's certainly better to use a single try/catch. For example:

function createCar()
{
    try {
      install_engine();
      install_brakes();
    } catch (Exception $e) {
        die("I could not create a car");
    }
}

多个尝试 / catch 块是有用的,如果你可以并打算以特定的方式来处理这个失败,究竟是什么导致的。

Multiple try/catch blocks are useful if you can and intend to handle the failure in a manner specific to what exactly caused it.

function makeCocktail()
{
    try {
        pour_ingredients();
        stir();
    } catch (Exception $e) {
        die("I could not make you a cocktail");
    }

    try {
        put_decorative_umbrella();
    } catch (Exception $e) {
        echo "We 're out of umbrellas, but the drink itself is fine"
    }
}

这篇关于如何有效地使用PHP中的try ... catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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