Example of Exception handling using php

Source: Internet
Author: User
Example of Exception handling using php

  1. Class Exception
  2. {
  3. Protected $ message = 'unknown exception'; // exception information
  4. Protected $ code = 0; // custom exception code
  5. Protected $ file; // file name with an exception
  6. Protected $ line; // The code line number with an exception
  7. Function _ construct ($ message = null, $ code = 0 );
  8. Final function getMessage (); // returns exception information
  9. Final function getCode (); // returns the exception code
  10. Final function getFile (); // returns the file name with an exception
  11. Final function getLine (); // return the code line number in which an exception occurs.
  12. Final function getTrace (); // backtrace () array
  13. Final function getTraceAsString (); // getTrace () information formatted as a string
  14. /* Methods that can be reloaded */
  15. Function _ toString (); // output string
  16. }
  17. ?>

Simple example: (throw an error message through an exception)

  1. Try {
  2. $ Error = 'My error! ';
  3. Throw new Exception ($ error)
  4. } Catch (Exception $ e ){
  5. Echo $ e-> getMessage ();
  6. }

2. extend this class

  1. Class MyException extends Exception
  2. {
  3. // Redefine the constructor to make the message a required attribute
  4. Public function _ construct ($ message, $ code = 0 ){
  5. // Custom code
  6. // Make sure all variables are correctly assigned values
  7. Parent: :__ construct ($ message, $ code );
  8. }
  9. // Customize the output style of the string
  10. Public function _ toString (){
  11. Return _ CLASS _. ": [{$ this-> code}]: {$ this-> message} \ n ";
  12. }
  13. Public function customFunction (){
  14. Echo "A Custom function for this type of exception \ n ";
  15. }
  16. }

The basic idea of exception handling is that the code is called and executed in try code. If an error occurs in the try block, we can execute a process that throws an exception. Some programming languages, such as java, will automatically throw exceptions under certain circumstances. In php, an exception must be thrown manually. Throw an Exception using the following method: Throw new Exception ('message', code); the Throw keyword triggers the Exception handling mechanism, which is a language structure rather than a function, however, you must pass a value to it. It requires an acceptance object. In the simplest case, you can instantiate a built-in Exception class. Finally, after the try code, at least one catch code block must be provided. Multiple catch code blocks can be associated with a try code block. If each catch code block can capture a different type of exception, it makes sense to use multiple catch code blocks. For example, to capture exceptions of the Exception class.

  1. Catch (Exception $ e)
  2. {
  3. // Handing exception
  4. }

The object captured by the Catch code is the object that causes an exception and is passed to the throw statement (thrown by the throw statement ). Exception class instances are a good choice. The Exception class provides the following built-in methods: Getcode ()-return the code passed to the constructor. GetMessage ()-return the message passed to the constructor. GetFile ()-The path to the file that generates the exception code is getLine ()-the row of the code that generates the exception is returned.

Note: When an exception is caught, subsequent code in the try () block will not continue to be executed, but will try to find the matched "catch" block. When an exception is thrown and no catch processing is performed, the "Uncaught Exception 'exception'" error is reported.

  1. Function test ($ val ){
  2. 'If ($ val> 100 ){
  3. Throw new Exception ("prompt message: Your input value is too large ");
  4. }
  5. }
  6. Test (111 );
  7. ?>

3. When an exception is thrown, whether the catch statement block is processed should be treated differently. Code of the user registration function

  1. Try {
  2. // Check forms filled in
  3. If (! Filled_out ($ _ POST )){
  4. Throw new Exception ('You have not entered the form, please enter it ');
  5. }
  6. // Check email address not valid
  7. If (! Check_email ($ email )){
  8. Throw new Exception ('invalid email format ');
  9. }
  10. // Check whether the density length is greater than 6
  11. If (strlen ($ passwd <6 )){
  12. Throw new Exception ('density length should be greater than 6 ');
  13. }
  14. // Check whether the two passwords are equal http://bbs.it-home.org
  15. If ($ passwd! = $ Passwd1 ){
  16. Throw new Exception ('Two passwords are different. please input them again ');
  17. }
  18. // Check whether the length of the user name is correct
  19. If (strlen ($ username)> 16 ){
  20. Throw new Exception ('The user name length does not match, please input it again ');
  21. }
  22. } Catch (Exception $ e ){
  23. Echo $ e-> getMessage (); // output exception information.
  24. }
  25. ?>

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.