0

私はプログラミング全般に非常に慣れていませんがtry {}、 PHPについて読んcatch {}でいるときに、その意味を誰かが助けてくれるかどうか疑問に思っていました。

4

2 に答える 2

2

例外のエントリーレベルの入門書:

function doSomething($arg1)
{
    if (empty($arg1)) {
        throw new Exception(sprintf('Invalid $arg1: %s', $arg1));
    }

    // Reached this point? Sweet lets do more stuff
    ....
}

try {

    doSomething('foo');

    // Above function did not throw an exception, I can continue with flow execution
    echo 'Hello world';

} catch (Exception $e) {
    error_log($e->getMessage());
}

try {

    doSomething();

    // Above function DID throw an exception (did not pass an argument)
    // so flow execution will skip this
    echo 'No one will never see me, *sad panda*';

} catch (Exception $e) {
    error_log($e->getMessage());
}

// If you make a call to doSomething outside of a try catch, php will throw a fatal
//  error, halting your entire application
doSomething();

関数/メソッド呼び出しをtry/catchブロック内に配置することで、アプリケーションのフロー実行を制御できます。

于 2012-04-05T04:24:50.147 に答える
1

PHP には非常に優れたドキュメントがあります。そこから始める必要があります。

http://php.net/manual/en/language.exceptions.php

于 2012-04-05T04:14:45.130 に答える