PHP Exception Handling

Source: Internet
Author: User
Tags continue exception handling getmessage modify php exception handling

The role of exception handling

Exception handling is used to change the normal flow of a script when a specified error (exception) condition occurs.

Basic syntax for exception handling:

try{
Code for errors (exceptions) that may occur
}
catch (Exception e) {
Catch an exception and handle it, usually by throwing an exception
Throwe;
}

actual requirements, there is a function that requires adduser () and Upduser () to succeed at the same time:

function A () {

AddUser (); Want to know if the AddUser () function succeeds and how to handle it?

Upduser (); Want to know if the Upduser () function succeeds and how to handle it?

}

How to solve?

(1) using the traditional method to determine by the return value;

<?php
Add usage
Required AddUser () and Upduser () were successful
function AddUser ($username) {
if ($username = = "Xiao Zhang") {
Add success
return true;
}else{
Failed
return false;
}
}
function Upduser ($username) {
if ($username = = "Xiao Li") {
Add success
return true;
}else{
Failed
return false;
}
}
function A () {
Calling methods, adding users
$res 1=adduser ("www.bianceng.cn");
Calling methods, modifying user
$res 2=upduser ("Hello");
if ($res 1 && $res 2) {
echo "modified successfully";
}else{
echo "failed to modify";
}
}
A ();
?>

(2) How to use exception handling mechanism?

<?php
/*//Add Usage
Required AddUser () and Upduser () were successful
function AddUser ($username) {
if ($username = = "Xiao Zhang") {
Add success
return true;
}else{
Failed
return false;
}
}
function Upduser ($username) {
if ($username = = "Xiao Li") {
Add success
return true;
}else{
Failed
return false;
}
}
function A () {
Calling methods, adding users
$res 1=adduser ("www.bianceng.cn");
Calling methods, modifying user
$res 2=upduser ("Hello");
if ($res 1 && $res 2) {
echo "modified successfully";
}else{
echo "failed to modify";
}
}
A ();
*/
try{
AddUser ("Xiao Zhang");
Upduser ("Hello");
}
Catch: the meaning of capturing; exception is a predefined exception class for PHP
catch (Exception $e) {
echo "Failure message =". $e->getmessage ();
}
function AddUser ($username) {
if ($username = = "Xiao Zhang") {
Add success
}else{
Failure throws an exception
throw new Exception ("Add failed");
}
}
function Upduser ($username) {
if ($username = = "Xiao Li") {
Add success
}else{
Failure throws an exception
throw new Exception ("Update failed");
}
}
?>

Considerations for Exception use:

By using the example above, you can see that the

try{
Code for errors (exceptions) that may occur
}
catch (Exception e) {
Catch an exception and handle it, usually by throwing an exception
Throwe;
}

This approach can be more effective in controlling errors, so it is used extensively in development.

Basic processing of exceptions

When an exception is thrown, subsequent code in the try () block does not continue, and PHP tries to find a matching "catch" code block. If an exception occurs, but there is no catch capture, it prompts a uncatched Exception

<?php
function Checknum ($val) {
if ($val >100) {
throw new Exception ("Error_no1: Value too large");
}else{
echo "The value entered is normal";
}
}
try{
Checknum (300);
}catch (Exception $e) {
echo $e->getmessage (). --$e->getline (). " Line ";
}
?>

When an exception is catch, it can be handled, or it can continue to be thrown (throw new Exception ("Information");).

Example: Number of rows showing errors

<?php
function Checknum ($val) {
if ($val >100) {
throw new Exception ("Error_no1: Value too large");
}else{
echo "The value entered is normal";
}
}
try{
Checknum (300);
}catch (Exception $e) {
echo $e->getmessage (). --$e->getline (). " Line ";
}
?>

You can also customize the exception handling class to replace the system default top-level exception handler:

<?php
Set_exception_handler ("My_exception"); To modify the default top-level exception handler
Customizing the top-level exception handler
function My_exception ($e) {
echo "My custom top-level exception handler". $e->getmessage ();
}
function Checknum ($val) {
if ($val >100) {
throw new Exception ("Error_no1: Value too large");
}else{
echo "The value entered is normal";
}
}
try{
Checknum (300);
}catch (Exception $e) {
echo $e->getmessage (). --$e->getline (). " Line ";
Continue to throw, at which point the PHP default exception handler will be started, or the top-level exception handler can be customized to handle
Throw $e;
}
?>

with multiple catch blocks, you can catch different kinds of exceptions :

<?php
An exception was customized
Class MyException1 extends exception{//inherits default exceptions
}
Class MyException2 extends exception{
}
function A () {
throw new MyException1 (a);
}
function B () {
throw new MyException2 (b);
}
function C () {
try{
A (); Throw MyException1
B (); Throw MyException2
}catch (Exception1 $e 1) {
echo $e 1->getmessage ();
}catch (Exception2 $e 2) {
echo $e 2->getmessage ();
}
}
?>

Some exceptions in PHP are not captured and can only be handled by the system exception handler. Can catch to an exception, depending on whether there is really an exception thrown.

Cases:

<?php
try{
$i =10/0;
$FP =fopen ("Abc.txt", "R"); Cannot be thrown, handled by system exception handler
}catch (Exception $e) {
echo "OK". <br/> "; The two lines of code did not execute because the code in the try block did not throw an exception
echo $e->getmessage ();
}
?>

Url:http://www.bianceng.cn/webkf/php/201701/50512.htm

Related Article

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.