【问题标题】:Convert if else to try catch转换 if else 以尝试 catch
【发布时间】:2010-11-25 06:11:58
【问题描述】:

有人可以帮助我将以下使用 if-else 编写的代码转换为 try/catch。也让我知道在这种情况下是否需要 trycatch 或者 if-else 很合适

$results = mysql_query($query);
if(mysql_num_rows($results)!=0)
{
    while(($result = mysql_fetch_row($results))!=FALSE)
    {
        $res   ="DELETE FROM table1 WHERE id ='".$result['id']."'";
        if(mysql_query($res)==false)
        {
            echo mysql_error();
            exit;
        }
    }
    echo $res   ="DELETE FROM table2 WHERE id ='".$id."'";
    if(mysql_query($res)!==false)
    {
        header("Location:list.php?m=4");
    }
    else
    {
        echo mysql_error();
        exit;
    }
}
else
{
    echo "Error";
}

【问题讨论】:

标签: php mysql try-catch if-statement


【解决方案1】:

try...catch 仅在您的函数抛出异常时才有意义。如果他们不这样做,catch 就没有任何意义。我将从重构开始:

$results = mysql_query($query);
if (!mysql_num_rows($results)) {
    echo 'No results!';
    exit;
}

$ids = array();
while (($result = mysql_fetch_row($results)) !== false) {
    $ids[] = $result['id'];
}

$ids = array_map('mysql_real_escape_string', $ids);
$query = "DELETE FROM table1 WHERE id IN ('" . join("','", $ids) . "')";
if (!mysql_query($query)) {
    echo mysql_error();
    exit;
}

$query = "DELETE FROM table2 WHERE id = '$id'";
if (!mysql_query($query)) {
    echo mysql_error();
    exit;
}

header("Location: list.php?m=4");
exit;

这仍然可以改进很多,但它已经比你的意大利面条逻辑有所改进。如果您对正确使用异常非常感兴趣,您应该首先继续正确使用函数来执行重复性任务(例如error, exit 部分),然后可能将整个事物重组为类和对象,最后使用异常在它们之间进行通信现在嵌套的图层。也许开始使用 PHP 框架来感受整个事情。

在上面的代码中加入异常只不过是一个goto,但只是为了说明目的

try {

    $results = mysql_query($query);
    if (!mysql_num_rows($results)) {
        throw new Exception('No results!');
    }

    $ids = array();
    while (($result = mysql_fetch_row($results)) !== false) {
        $ids[] = $result['id'];
    }

    $ids = array_map('mysql_real_escape_string', $ids);
    $query = "DELETE FROM table1 WHERE id IN ('" . join("','", $ids) . "')";
    if (!mysql_query($query)) {
        throw new Exception(mysql_error());
    }

    $query = "DELETE FROM table2 WHERE id = '$id'";
    if (!mysql_query($query)) {
        throw new Exception(mysql_error());
    }

    header("Location: list.php?m=4");
    exit;

} catch (Exception $e) {

    echo 'ERROR: ' . $e->getMessage();
    exit;

}

【讨论】:

  • 谢谢 deceze 你能解释一下 catch 部分吗 try { ....... } catch (Exception $e) { echo 'ERROR: ' 。 $e->getMessage();出口; }
  • @Web 您应该阅读有关异常的教程,但简而言之:如果在 try 块内引发任何异常,则块中的任何以下代码都将被跳过,catch 块将被执行。抛出的异常将作为$e 传递给catch 块。 $e 是一个具有getMessage 方法的对象,它返回创建时传递给它的消息。 IE。消息将是“没有结果!”对于throw new Exception('No results!') 异常。
【解决方案2】:

从它的声音来看,您似乎认为 try/catch 和 if-else 是相同的行为。事实并非如此。 Try catch 用于防止异常导致应用程序崩溃或优雅地处理异常,以及执行日志记录和提供用户反馈。 if-else (else if) 用于检查应用程序的内部状态,并相应地执行不同的操作。

一般来说,try-catch 的效率低于使用 switch-case 或 else-if 方法来解决问题。

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2011-03-30
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多