Execution order of Try.catch and return in PHP

Source: Internet
Author: User
Tags php language try catch
try {      $pdo->begintransaction ();      foreach ($sql _arr as $sql) {            $stmt = $pdo->exec ($sql);      }      $pdo->commit ();      return true;} catch (Exception $e) {      echo $e->getmessage ();      $pdo->rollback ();      return false;}


Above Please be familiar with the great God to help explain, in the PHP language, if there is a return in the try Catch statement, what is the order of execution?
Above this code, want is: PDO thing normal run return true, if have an exception return false, can I ask if the normal implementation?


Reply to discussion (solution)

This is what you write: normal run returns TRUE if an exception returns false

But be sure to set the property entry Pdo::attr_errmode to Pdo::errmode_exception
Otherwise, exception handling is not valid

And also
Exception $e
To write
Pdoexception $e

There's a lot of trouble!

Moderator greatly has answered, I come to mix, a small example

$res =test (' oo '), if ($res) {echo ' hehe ';} function test ($name) {try{a ($name); return true;} catch (Exception $e) {echo $e->getmessage (); return false;}} function A ($name) {if ($name = = "xx") {return true;} Else{throw New Exception ("Error");}}

This is what you write: normal run returns TRUE if an exception returns false

But be sure to set the property entry Pdo::attr_errmode to Pdo::errmode_exception
Otherwise, exception handling is not valid

And also
Exception $e
To write
Pdoexception $e

There's a lot of trouble!


Thank!
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.