PHP Learning Try Catch

Source: Internet
Author: User
Tags getmessage try catch vars

PHP 5 adds exception handling modules similar to those in other languages. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement . ( Note: Must be thrown first to obtain)
The code that requires exception handling must be placed inside a try code block to catch possible exceptions.
Each try must have at least one catch corresponding to it.
You can use multiple catches to catch exceptions that are generated by different classes.
When the try code block no longer throws an exception or cannot find a catch that matches the thrown exception, the PHP code resumes execution after jumping to the last catch.
Of course, PHP allows you to throw (throw) exceptions again within a catch code block.
When an exception is thrown, the code behind it (the code block where the exception was thrown) will not continue, and PHP will attempt to find the first catch to match it.

If an exception is not captured and does not use Set_exception_handler () for the appropriate processing, then PHP will produce a serious error and output uncaught exception ... (The exception is not caught) prompt information.

Let's take a look at the basic properties and methods of the PHP built-in exception class. (not including specific implementations)

[PHP]View Plaincopy
  1. <?php
  2. /**
  3. * exception.php
  4. *
  5. * PHP5 properties and methods of the built-in exception classes
  6. * The following code is only for the purpose of explaining the structure of the built-in exception-handling class, which is not a meaningful piece of code available.
  7. */
  8. Class exception{
  9. protected $message = ' Unknown exception '; //exception information
  10. protected $code = 0; //user Custom exception code
  11. protected $file; //filename of exception occurred
  12. protected $line; //code line number where the exception occurred
  13. function __construct ($message = null, $code = 0);
  14. Final function getMessage (); //Return exception information
  15. Final function GetCode (); //Return exception code (codename)
  16. Final function getFile (); //Returns the file name of the exception that occurred
  17. Final function getLine (); //Returns the code line number where the exception occurred
  18. Final function gettrace (); ///BackTrace () array
  19. Final function gettraceasstring (); //Gettrace () information that has been rasterized into a string
  20. //methods that can be overloaded
  21. function __tostring (); //output-able string
  22. }
  23. ?>


Examples are as follows:

Include file error throwing exception
<?php
The wrong demo
try {
Require (' test_try_catch.php ');
} catch (Exception $e) {
echo $e->getmessage ();
}


Correct throwing of exceptions
try {
if (file_exists (' test_try_catch.php ')) {
Require (' test_try_catch.php ');
} else {
throw new Exception (' file is not exists ');
}
} catch (Exception $e) {
echo $e->getmessage ();
}

PHP Learning Try 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.