PHP exception handling mechanism

Source: Internet
Author: User
Tags getmessage php exception handling

1. Exceptions:

An exception (Exception) is used to change the normal process of a script when a specified error occurs.

When an exception is triggered, it usually occurs:

(1) The current code state is saved;

(2) Code execution is switched to predefined exception handler functions;

(3) Depending on the situation, the processor may restart execution of the code from the saved code State, terminate the script execution, or continue executing the script from another location in the code.

2. Basic use of exceptions:

When the exception is thrown, the subsequent code does not continue, andPHP tries to find a matching "Catch" block.

If the exception is not captured and does not use Set_exception_handler () for appropriate processing, a critical error (fatal error) will occur and the output "uncaught Exception " (uncaught exception) error message.

The correct processing procedure should include:

(1)try- the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.

(2)throw- This specifies how the exception is triggered. Each "throw" must correspond to at least one "Catch".

(3)catch-the "catch" code block catches an exception and creates an object that contains the exception information.

3. Create a custom Exception class:

The custom Exception class that you create must be an extension of the Exception class.

when the exception thrown by the throw is a subclass exception class object, acatch catch exception can use the subclass exception class or use the base class exception class;the exception thrown by the throw is the base class exception class object. Catch catches exceptions can only use the base class exception class, if the use of subclass exception class will be an error.

4. Multiple Exceptions

You can use multiple exceptions for a script to detect multiple situations.

You can use multiple if: Else code block, or a switch code block, or nested multiple exceptions. These exceptions can use different exception classes and return different error messages.

5. re-throwing Exceptions

Sometimes, when an exception is thrown, you might want to handle it in a different way from the standard. You can throw an exception again in a "catch" code block.

6. Set the top-level exception handler:

  Set_exception_handler () function to set a user-defined function that handles all uncaught exceptions.

Example:

function MyException ($exception)

{

echo "<b>Exception:</b>", $exception->getmessage ();

}

Set_exception_handler (' myexception ');

throw new Exception (' uncaught Exception occurred ');

  Note: Theset_exception_handler setting must be preceded by an exception before an error occurs.

7. Rules for exceptions:

(1) code that requires exception handling should be placed inside a try code block to catch a potential exception.

(2) Each try or throw code block must have at least one corresponding catch code block.

(3) multiple catch code blocks can be used to catch different kinds of exceptions.

(4) An exception can be thrown again (re-thrown) in a catch code block within a try code block .

In short: If an exception is thrown, it must be captured.

code example: (Customizing an exception class, throwing different exceptions)

Class MyException extends Exception

{

Public Function errormsg ()

{

$message = $this->getmessage (). "Haha";

return $message;

}

}

$num = 0;

Try

{

if ($num > 1)

{

throw new Exception ("Value must be 1 or below");

}

Else

{

throw new MyException ("Value is OK");

}

}

catch (MyException $e)

{

echo $e->errormsg ();

}

catch (Exception $e)

{

echo $e->getmessage ();

}

PHP exception handling mechanism

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.