PHP Exception Catch handling

Source: Internet
Author: User
Tags error handling exception handling stack trace

The exception is used to change the normal flow of the script, if the specified error


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

what is an exception
In PHP 5 comes a way to handle a new object in an error-oriented manner.

exception handling is used to change the execution of code if the specified error (Exception normal flow) condition occurs. This situation is called an exception.

This is what happens when an exception is usually fired:

Current state Save Code
the execution of the code switches to the predefined (custom) exception handler function
in this case, the processing can recover the saved code executed by the State, terminate or continue executing the script from different locations in the code
we will show different error handling methods:

Basic usage Exceptions
Create a custom exception handler
more exceptions
Throwing Exceptions again
setting top-level exception handling
Note: Exceptions should only use error conditions and should not be used in another place in the specified hop-point code.


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

Basic usage of exceptions
When an exception is thrown, the following code is not executed, and PHP will attempt to find a matching "catch" block.

If an exception is not captured, a fatal error emits a "catch exception" message.

let's try to throw the exception out without scratching:

<?php
//create function with an exception
function Checknum ($number)
  {
  if ($number >1)
    {
    throw New Exception ("Value must be 1 or below");
   &NBSP;&NBSP}
  return true;
  

//trigger exception
Checknum (2);
?>

Fatal Error: uncaught exception ' Exception '
With message ' Value must is 1 or below ' in C:webfoldertest.php:6
Stack Trace: #0 c:webfoldertest.php (12):
Checknum #1 {main} thrown in c:webfoldertest.php on line 6

Try, throw and catch


To avoid the error in the above example, we need to create the appropriate code to handle the exception.

The appropriate exception code should include:

Try-The function uses an exception that should be in a "try" block. If the exception does not trigger the code will continue to be normal. However, if an exception is fired, an exception "throws"
Throw--This is how the exception is triggered. Each "throw" must have at least one "capture"
Capture-a "catch" block retrieves an exception, creates an object, contains exception information
Let's try to trigger a valid code except

 <?php
//create function with a exception
function Checknum ($number)
  {
 & Nbsp;if ($number >1)
    {
    throw new Exception ("Value must be 1 or below ");
    }
  return true;
  }

//trigger exception in a ' try ' block
try
  {
  checknum (2);
  //i F The exception is thrown, this text won't be shown
  echo ' If you are here, the number is 1 or below ';   }

//catch exception
catch (Exception $e)
  {
  echo ' message: '. $ E->getmessage ();
  }
?

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.