Basic php exception handling methods

Source: Internet
Author: User
Tags php exception handling
The following describes how to handle exceptions in php. For more information about how to handle exceptions, see this tutorial. The following is a reference segment: 1. allow a method to provide a detailed error mark to the client code 2.

The following describes how to handle exceptions in php. For more information about how to handle exceptions, see this tutorial.

The following is a reference clip:

1. allow a method to give an error mark to the customer code

2. provide detailed information about program errors

3. let you determine multiple error conditions at the same time and separate your error report from the program processing process.

4. the return value must be of an independent type and will not be confused with the normal returned type.

Two instances master PHP exception handling

Example [1] use try... catch

The instance code is as follows:

  1. /* PDO connects to the mysql database. if you have not read the PDO, first check the PDO constructor. otherwise, skip example 1 and see Example 2 */
  2. $ Dsn = 'MySQL: host = localhost; dbname = testdb ';
  3. $ User = 'dbuser ';
  4. $ Password = 'dbpass ';
  5. Try {
  6. $ Dbh = new PDO ($ dsn, $ user, $ password); // An exception may occur when you create a database connection object.
  7. Echo 'If an exception occurs above, I cannot be displayed ';
  8. } Catch (PDOException $ e ){
  9. Echo 'connection failed: '. $ e->__ toString ();
  10. }
  11. ?>

The instance code is as follows:

  1. // Example [2] try... cathc and throw
  2. Try {
  3. $ Error = 'I throw exception information and exit the try block ';
  4. If (is_dir ('./tests ')){
  5. Echo 'Do something .';
  6. } Else {
  7. Throw new Exception ($ error, 12345 );
  8. }
  9. Echo 'if there is an exception above, it won't turn to me !~ ', "N ";
  10. } Catch (Exception $ e ){
  11. Echo 'capture exception: ', $ e-> getMessage (), $ e-> getCode (), "n "; // Display $ error and 123456
  12. }
  13. Echo 'continue execution ';
  14. ?>

As a friend said above, I wrote some program exercises for myself:

The instance code is as follows:

  1. $ A = 20;
  2. // Example of using regular catch
  3. Try
  4. {
  5. If ($ a = 1)
  6. {
  7. Throw new Exception ("I am 1", 1 );
  8. }
  9. Elseif ($ a = 2)
  10. {
  11. Throw new Exception ("I am 2", 3 );
  12. }
  13. Elseif ($ a = 3)
  14. {
  15. Throw new Exception ("I am 3", 3 );
  16. }
  17. Elseif ($ a = 4)
  18. {
  19. Throw new Exception ("I am 4", 4 );
  20. }
  21. Else
  22. {
  23. Throw new Exception ("Who am I? ", 0 );
  24. }
  25. }
  26. Catch (Exception $ e)
  27. {
  28. If ($ e-> getCode () = 1)
  29. {
  30. Echo "1:". $ e;
  31. }
  32. Elseif ($ e-> getCode () = 2)
  33. {
  34. Echo "2:". $ e;
  35. }
  36. Elseif ($ e-> getCode () = 3)
  37. {
  38. Echo "3:". $ e;
  39. }
  40. Elseif ($ e-> getCode () = 4)
  41. {
  42. Echo "4:". $ e;
  43. }
  44. Else
  45. {
  46. Echo "0:". $ e;
  47. }
  48. }
  49. Echo"
  50.  
  51. --------------------------------------------------------------------------------
  52. ";
  53. // Example of using different extended exception classes
  54. Class A extends Exception {};
  55. Class B extends Exception {};
  56. Try
  57. {
  58. If ($ a = 1)
  59. {
  60. Throw new A ("I am A", 1 );
  61. }
  62. Elseif ($ a = 2)
  63. {
  64. Throw new B ("I am B", 2 );
  65. }
  66. Else
  67. {
  68. Throw new Exception ("Who am I? ", 0 );
  69. }
  70. }
  71. Catch (A $ e)
  72. {
  73. Echo "1:". $ e;
  74. }
  75. Catch (B $ e)
  76. {
  77. Echo "2:". $ e;
  78. }
  79. Catch (Exception $ e)
  80. {
  81. Echo "0:". $ e;
  82. }
  83. ?>

The output is as follows:

The following is a reference clip:

0: exception 'exception' with message 'Who am I? 'In |

D: WebPHPWWWnewtest. php: 24 Stack trace: #0 {main}

----------------------------------------------------------------------------

0: exception 'exception' with message 'Who am I? 'In D: WebPHPWWWnewtest. php: 69 Stack trace: #0 {main}

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.