Home > Backend Development > PHP Tutorial > Example of using php exception handling class Exception

Example of using php exception handling class Exception

WBOY
Release: 2016-07-25 09:05:31
Original
1318 people have browsed it
  1. class Exception
  2. {
  3. protected $message = 'Unknown exception'; // Exception message
  4. protected $code = 0; // User-defined exception code
  5. protected $file; // Occurrence Exception file name
  6. protected $line; // Code line number where the exception occurred
  7. function __construct($message = null, $code = 0);
  8. final function getMessage(); // Return exception information
  9. final function getCode() ; // Return the exception code
  10. final function getFile(); // Return the file name where the exception occurred
  11. final function getLine(); // Return the code line number where the exception occurred
  12. final function getTrace(); // backtrace() array
  13. final function getTraceAsString(); // GetTrace() information that has been formatted into a string
  14. /* Overloadable method*/
  15. function __toString(); // Outputable string
  16. }
  17. ?>
Copy code

Simple example: (Throw error message through exception)

  1. try {
  2. $error = 'my error!';
  3. throw new Exception($error)
  4. } catch (Exception $e) {
  5. echo $e->getMessage() ;
  6. }
Copy code

2. Expand this class

  1. class MyException extends Exception
  2. {
  3. // Redefine the constructor to make message a property that must be specified
  4. public function __construct($message, $code = 0) {
  5. // Customized code
  6. // Make sure all variables are assigned correctly
  7. parent::__construct($message, $code);
  8. }
  9. // Customize the style of string output
  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 exceptionn";
  15. }
  16. }
Copy code

The basic idea of ​​exception handling is that the code is called and executed in the try code. If an error occurs in the try code block, we can perform an exception handling. Some programming languages, such as Java, will automatically throw exceptions under certain circumstances. In PHP, exceptions must be thrown manually. An exception can be thrown as follows: Throw new Exception('message',code); The Throw keyword will trigger the exception handling mechanism. It is a language construct, not a function, but a value must be passed to it. It requires a receiving object. In the simplest case, a built-in Exception class can be instantiated. Finally, after the try code, at least one catch code block must be given. You can associate multiple catch code blocks with a try code block. It makes sense to use multiple catch blocks if each catch block can catch a different type of exception. For example, if you want to catch exceptions of the Exception class.

  1. Catch(Exception $e)
  2. {
  3. //handing exception
  4. }
Copy code

The object captured by the Catch code is the object that caused the exception and was passed to the throw statement (Thrown by throw statement). Using an instance of the Exception class is a good choice. The Exception class provides the following built-in methods: Getcode() —Returns the code passed to the constructor. GetMessage() — Returns the message passed to the constructor. getFile() - Returns the path to the file that generated the exception code getLine() - Returns the line of code that generated the exception.

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

  1. function test($val){
  2. if ($val>100){
  3. throw new Exception("Message: The value you entered is too large");
  4. }
  5. }
  6. test(111);
  7. ?>
Copy code

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

  1. try{
  2. //check forms filled in
  3. if(!filled_out($_POST)){
  4. throw new Exception('You haven't filled in the form yet, please go back and fill it in');
  5. }
  6. //check email address not valid
  7. if(!check_email($email)){
  8. throw new Exception('The format of the email is incorrect');
  9. }
  10. //Check whether the length of the density is greater than 6
  11. if(strlen ($passwd<6)){
  12. throw new Exception('The length of the density 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('The two passwords are different, please re-enter');
  17. }
  18. //Check whether the length of the user name is correct
  19. if(strlen($username)>16){
  20. throw new Exception('The length of the user name does not match, please re-enter');
  21. }
  22. } catch(Exception $e){
  23. echo $e->getMessage(); //Output exception information.
  24. }
  25. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template