简体   繁体   English

PHP异常尝试捕获

[英]PHP exceptions try and catch

If I were to do the following: 如果我要执行以下操作:

$pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");

$pds->execute(array(':username' => $username, ':password' => $password));

$row = $pds->fetch(PDO::FETCH_ASSOC);

Do I need to put a try {} for each command executed, or will a try block cover the entire code, with a single catch block? 我是否需要为每个执行的命令放入try {},还是将try块用单个catch块覆盖整个代码?

Thanks! 谢谢!

Do I need to put a try {} for each command executed, or will a try block cover the entire code, 我是否需要为每个执行的命令放置一个try {},还是将try代码块覆盖整个代码,

Neither. 都不是。

Here goes a set of proper rules right from the real life: 这里有一系列来自现实生活的适当规则:

  • use try/catch for the single command or set of commands only in case of 在以下情况下对单个命令或命令集使用try / catch
    • you're going to handle the error itself . 您将自行处理错误 Frankly, if you have an action to do in case of error: to rollback a transaction, for example. 坦白说,如果您在发生错误的情况下要采取行动 ,例如:回滚事务。
    • error is non-fatal - to bypass it 错误不是致命的-绕过它
  • all other exceptions have to be handled by exception handler , not global try/catch block. 所有其他异常必须由异常处理程序处理 ,而不是全局try / catch块。

though you can omit the latter one, as PHP has built-in basic handler that works better than one an inexperienced programmer can develop. 尽管您可以省略后者,但是PHP具有内置的基本处理程序,该处理程序比没有经验的程序员可以开发的更好。

A try block will fail catch the first exception that is generated. 一个try块将无法捕获生成的第一个异常。 Therefore it is quite safe to place all 3 statements in the try section. 因此,将所有3条语句放在try部分中是非常安全的。

You can also use multiple catch blocks so that different exception types can be handled differently such as: 您还可以使用多个catch块,以便可以对不同的异常类型进行不同的处理,例如:

 try {
      $pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");
      $pds->execute(array(':username' => $username, ':password' => $password));
      $row = $pds->fetch(PDO::FETCH_ASSOC);
 } catch (PDOException $e) {
      echo 'A pdo exception happened';
 } catch (Exception $e) {
      echo 'A different exception happened';
 }

This helps ensure you can for example clean up after the issue. 这有助于确保您可以在问题后进行清理。

what you would need to do for the query you are doing is: 您需要为正在执行的查询做的是:

try{
  $pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");
  $pds->execute(array(':username' => $username, ':password' => $password));
}
catch(PDOException $ex){
  die("Failed to run query: " . $ex->getMessage());
  //Or Echo, or store in a variable to process if you don't want to die()
}

$row = $pds->fetch(PDO::FETCH_ASSOC);

Hope this helps! 希望这可以帮助!

Edit: Also, if you want a bit more separation and readability for building a query you can try creating a query parameter array instead of creating the array directly in the execute() function. 编辑:另外,如果您希望更多的分离性和可读性来构建查询,则可以尝试创建查询参数数组,而不是直接在execute()函数中创建数组。

$pds = $pdo->prepare("SELECT * FROM userinfo WHERE username=:username AND   password=:password");
$query_params = array(
  ':username' => $username,
  ':password' => $password
);
$result = $pds->execute($query_params);

You should definitely study error handling. 您绝对应该研究错误处理。 Also, you should do a little research (at least on stackoverflow) before posting this type of questions. 另外,在发布此类问题之前,您应该做一点研究(至少在stackoverflow上)。

You can put that code inside a single try block. 您可以将该代码放在单个try块中。

try
{
$pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND       password=:password");

$pds->execute(array(':username' => $username, ':password' => $password));

$row = $pds->fetch(PDO::FETCH_ASSOC);
}
catch(Exception $ex)
{


}

You need only to place a try{} block around the all code, and catch it with a single catch{} block. 您只需要在所有代码周围放置一个try{}块,并使用单个catch{}块对其进行catch{} See the php manual for more information. 有关更多信息,请参见php手册

As all of the methods will potentially throw the same exception: PDOException it could make sense to wrap each call it is own try/catch block. 由于所有方法都可能引发相同的异常: PDOException因此可以将每个调用包装为自己的try/catch块是有意义的。 Yes, this is a good idea if you need to react depending on which method throws the exception and don't to parse the exception's errorInfo and/or errorCode (which will be driver dependend) 是的,如果您需要根据哪个方法引发异常并且不解析异常的errorInfo和/或errorCode (将作为驱动程序的依赖)进行响应,那么这是一个好主意。

One try/catch block means, if you get exception on first statement, remaining ones will not be executed, which is obvious to do in your case here. 一个try / catch块意味着,如果您在第一条语句上遇到异常,其余的将不会执行,这显然在您的情况下可以执行。

In future if you have a different situation, your choice can be different too. 将来如果您有其他情况,您的选择也可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM