PHP error details

Source: Internet
Author: User
What are PHP exceptions? PHP5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception. & Nbsp; general usage: & lt ;? Phpfunctiontest () {thrownewException (& quot; abnormal & quot;);} try {PHP exception details

What are PHP exceptions?

PHP 5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception.

General usage:

 GetMessage ();}
  1. Throw new Exception ('XXX'): throws an Exception.
  2. Try: the abnormal function should be located in the "try" code block. If no exception is triggered, the code continues as usual. However, if an exception is triggered, an exception is thrown.
  3. Catch: the code block captures exceptions and creates an object containing exception information.

Custom exception class:

class myException extends Exception { public function errorMessage() {  $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()  .': '.$this->getMessage().' is not a valid E-Mail address';  return $errorMsg;  }}try {  throw new myException($email);}catch (myException $e){ echo $e->errorMessage();}
  1. The myException class is a custom Exception class and must inherit Exception
  2. Exception is a built-in Exception class in PHP. by default, the default built-in Exception class in PHP is used. However, we can customize it, custom exception classes that meet your needs.
  3. When a custom exception class is used, throw new myException also needs to throw a custom exception class name.
  4. You also need to capture custom exception classes during catch.
  5. If an exception is thrown and not captured, an error occurs in the PHP code, as shown below:
Fatal error: Uncaught exception 'myException' in D:\AppServ\www\cctv\trunk\index.php:12 Stack trace: #0 {main} thrown in D:\AppServ\www\cctv\trunk\index.php on line 12


PHP default exception class details:

Class exception {protected $ message = 'unknow exception'; // custom exception information protected $ code = 0; // The defined exception code protected $ file; // the name of the abnormal PHP program protected $ line; // PHP row number in which an exception occurs // The Constructor used to pass user-defined exception information and user-defined exception code _ construct ($ message = null, $ code = 0); final function getMessage (); final function getCode (); final function getFile (); final function getLine (); final function getTrace (); // return the exception passing route in array form final function getTraceAsString (); // return the getTrace function information formatted as a string function _ toString (); // reload, used to return output strings}
  1. The default PHP exception class contains many custom variables and methods.
  2. $ E-> getMessage generally gets error information
  3. $ E-> getCode () is generally used to obtain error codes.
  4. $ E-> getFile () get the wrong file information
  5. $ E-> getLine () get the wrong number of rows
  6. $ E-> getTrace () file path after an exception
  7. _ ToString () can be reloaded to return output strings.

PHP exception class:

  1. Capture exceptions. you can throw an exception when MYSQL cannot be connected or the service logic is incorrect and receive the exception at the top of the code.
  2. Exception analysis and processing. When an exception occurs, you can capture the error information and write the log.
  3. You can return friendly prompts. for example, the WEB side can return the form of a page, or the AJAX side can return the JSON data format.
  4. Principle: if an exception is thrown, it must be captured; otherwise, an error will be reported in PHP.


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.