Exception Handling try-catch-finally_php Tutorial

Source: Internet
Author: User
Tags exit in finally block getmessage try catch
php5.5 New finally module

try {
Good work, there's a problem, don't be afraid.
} catch (HttpException $e) {
Always ready to handle the HTTP issues thrown above
} catch (Exception $e) {
Always ready to deal with problems they can't handle.
} finally {
Clean the battlefield, pack up and leave.
}


Finally will continue execution after return in try, and if there is return in Finally, the final return value is the value of return in finally.
Finally does not execute after die or exit in try.

EXAMPLE01:

 Php/*The *finally block is a good design in which the return statement overwrites the return statement in the other block and handles the exception thrown by the try catch without try-catch nesting to handle the exception thrown by the child try-catch. This is the same as Java, C # The presence of a return statement in a finally block throws compile time error.*/functionasdf () {Try {        Throw New Exception(' ERROR '); }    Catch(Exception $e) {        Echo"An error occurred"; Throw $e; } finally {//This overrides the exception as if it were never thrown        return"\nexception erased"; }}Try {    Echoasdf ();}Catch(Exception $e) {    Echo"\nresult:".$e-getMessage ();}/*The output from above would look like This:an error occurred Exception erased without the return statement I n the finally block it would look like This:an error occurred result:error*/


EXAMPLE02:

 
  PHP/* has a counterintuitive behavior in PHP 5.5.3 ' s finally and return statements: In one method, the try block returns a variable, and the finally block modifies the variable, The return is finally modified, but when the variable that the TRY block returns participates in the operation (evaluated In-line), the finally block's modification to the variable is ignored (the reason is unknown ...). )*/function$foo =1tryreturn $foo  $foo+ +function$foo =1try  return $foo +0 $foo + + $test 1  // returns 2, not the correct value of 1. $test 2 // returns correct value of 1, but inconsistent with $test 1.


EXAMPLE03:

 Php/** Small example Verify variable check if the name contains only letters, and does not contain the word name*/$name= "Name";Try{        Try        {                //Preg_match () returns the number of occurrences of pattern. Its value will be 0 times (mismatched) or 1 times, because Preg_match () will stop the search after the first match. Preg_match_all () differs from this, and it searches subject until the end is reached. If an error occurs, Preg_match () returns FALSE.                 if(Preg_match('/[^a-z]/i ',$name))                {                        Throw New Exception("$nameContains character other than A-Z "); }                if(Strpos(Strtolower($name), ' name ')!==false)                {                        Throw New Exception("$nameContains the word name "); }                Echo"The Name is valid"; }        Catch(Exception $e)        {                Throw New Exception("Insert name Again", 0,$e); }}Catch(Exception $e){        if($e-getprevious ()) {                Echo"The Previous Exception is:".$e->getprevious ()->getmessage (). "
"; } Echo"The Exception is:".$e->getmessage (). "
";}


Example04

 
  PHP/*whencatching an exception inside a namespace it's important that's escape to the global space: How to escape from life Name Space */class  someclass  {function  SomeFunction () {    try  {    thrownewException(' Some Error Message ') );    Catch (\Exception$e) {    var_dump($e,getMessage ()) ;   }  } }// Error://fatal error:class ' somenamespace\exception ' not found in C:\xampp\htdocs\tonglei\ index.php on line 8


Example05

 
  PHP// The following wording will be reported T_throw Syntax Error. Throw New Exception (); // This can be written in the correct form function throwexception ($messagenull,$codenull) {     Throw New Exception ($message,$code);} SomeFunction () or throwexception ();

http://www.bkjia.com/PHPjc/769758.html www.bkjia.com true http://www.bkjia.com/PHPjc/769758.html techarticle php5.5 New Finally module try {//good dry, out of the question do not be afraid, outside someone to catch} (HttpException $e) {//time ready to handle the above-thrown HTTP problem} catch (...

  • 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.