异常处理

PHP 5 has an exception model similar to that of other programming languages. An exception can bethrown, and caught("catched")within PHP. Code may be surrounded in atryblock, to facilitate the catching of potential exceptions. Eachtrymust have at least one correspondingcatchorfinallyblock.

The thrown object must be an instance of theExceptionclass or a subclass ofException. Trying to throw an object that is not will result in a PHP Fatal Error.

catch

Multiplecatchblocks can be used to catch different classes of exceptions. Normal execution(when no exception is thrown within thetryblock)will continue after that lastcatchblock defined in sequence. Exceptions can bethrown(or re-thrown)within acatchblock.

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matchingcatchblock. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined withset_exception_handler().

淘宝 adsense 商品推荐

Google AdSense实战宝典 用谷歌广告联盟出海赚美元 第2版 祁劲松 网站运营书籍 seo搜索引擎优化 广告营销 AdSense从入门到精通

价格:41.08元

finally

In PHP 5.5 and later, afinallyblock may also be specified after or instead ofcatchblocks. Code within thefinallyblock will always be executed after thetryandcatchblocks, regardless of whether an exception has been thrown, and before normal execution resumes.

注释

Internal PHP functions mainly useError reporting, only modernObject orientedextensions use exceptions. However, errors can be simply translated to exceptions withErrorException.

范例

Example #3 Throwing an Exception

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo "Hello World\n";
?>

以上例程会输出:

0.2
Caught exception: Division by zero.
Hello World

Example #4 Exception handling with afinallyblock

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

try {
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "Second finally.\n";
}

// Continue execution
echo "Hello World\n";
?>

以上例程会输出:

0.2
First finally.
Caught exception: Division by zero.
Second finally.
Hello World

Example #5 Nested Exception

<?php

class MyException extends Exception { }

class Test {
    public function testing() {
        try {
            try {
                throw new MyException('foo!');
            } catch (MyException $e) {
                // rethrow it
                throw $e;
            }
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
    }
}

$foo = new Test;
$foo->testing();

?>

以上例程会输出:

string(4) "foo!"

淘宝 云主机 商品推荐

远程租电脑出租e5云服务器主机租用出租游戏高防物理机单双路ECS

价格:10元