Brother even new version of PHP video tutorial data sharing

Source: Internet
Author: User
Tags php exception handling try catch
"Brother even new version of PHP video tutorial" Course, PHP (Foreign name: Php:hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. The grammar absorbs the C language, Java and Perl features, is conducive to learning, widely used, mainly for the field of web development. PHP's unique syntax mixes the syntax of C, Java, Perl, and PHP's own creation. It can execute Dynamic Web pages more quickly than CGI or Perl. Dynamic pages made in PHP are compared to other programming languages, and PHP is executed in HTML (an application under the standard Universal Markup Language), which is much more efficient than CGI, which generates HTML markup entirely; PHP can also execute post-compilation code, Compilation can achieve encryption and optimize code execution, making code run faster.

Course Play Address: http://www.php.cn/course/358.html

The teacher's lecture style:

Teachers teach in a clear, well-organized, layered analysis, interlocking, rigorous argumentation, structural rigorous, with the logical power of thinking to attract students ' attention, with reason to control the course of classroom teaching. Teaching skills, full of wit, all kinds of teaching methods, skills, at ease, just right, and without the slightest trace of the carving.

The more difficult in this video should be: PHP exception handling:

Exception handling, also known as error handling, provides a way to handle errors or exceptions that occur when a handler runs.

Exception handling is usually the action taken to prevent an unknown error from arising. The advantage of exception handling is that you don't have to work your brains out to think about all kinds of errors, which provides a very effective way to deal with a certain kind of error, which greatly improves the efficiency of programming. When an exception is triggered, it usually occurs:
Current code state is saved
Code execution is switched to the pre-defined exception handler function
Depending on the situation, the processor may start executing the code again from the saved code state, terminating the script execution, or continuing the script from another location in the code

PHP 5 provides a new approach to object-oriented error handling. You can use Instrumentation (try), throw (throw), and catch (catch) exceptions. Even if the throw (throw) exception is detected with a try, the catch is used to catch the exception if an exception is thrown (throw).

A try must have at least one catch corresponding to it. You can capture different objects by defining multiple catch. PHP executes in the order in which the catch is defined, until the last one is completed. Within these catch, a new exception can be thrown.

1. Use of exceptions

When an exception is thrown, the subsequent code will not proceed, and PHP will attempt to find a matching "catch" block of code. If an exception is not captured and is not used with Set_exception_handler (), then PHP will produce a serious error and the output fails to catch the exception (Uncaught exception ...) Prompt for information.

Throws an exception, but does not capture it:

<?php  ini_set (' display_errors ', ' on ');  Error_reporting (E_all & ~ e_warning);  $error = ' Always throw this error ';  throw new Exception ($error);  Continue to execute  Echo ' Hello world ';  ? >

The above code will get a fatal error like this:

Fatal error:uncaught Exception ' exception ' with message ' always throw the This error ' in E:\sngrep\index.php on line 5  Ex Ception:always throw this error in E:\sngrep\index.php on line 5 call  Stack:      0.0005     330680   1. {main} () e:\sngrep\index.php:0

2. Try, throw and catch

To avoid this fatal error, you can capture it using a try catch.

The processing handler should include:
Try-the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
Throw-this specifies how the exception is triggered. Each "throw" must correspond to at least one "catch"
Catch-the "catch" code block catches an exception and creates an object that contains the exception information

Throws an exception and catches it, you can proceed with the following code:

<?php  try {      $error = ' Always throw this error ';      throw new Exception ($error);        From here, the code within the TRA code block will not be executed by      Echo ' never executed ';    } catch (Exception $e) {      echo ' caught Exception: ',  $e- >getmessage (), ' <br> ';  }    Continue to execute  Echo ' Hello world ';  ? >

The "Try" code block detects that there are no throws "throw" exceptions, and throws an exception here.
The catch code block receives the exception and creates an object ($e) that contains the exception information.
Output the error message from this exception by calling $e->getmessage () from this exception object
To follow the principle that each throw must correspond to a catch, you can set up a top-level exception handler to handle the missing error.

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.