phpの異常処理try catch

684 ワード

1、簡単な使い方
try{
    $redis = new redis();
    if($redis->connect('127.0.0.1','6378')){

    }else{
        throw new Exception('  redis     ');
    }
}catch(Exception $e){
    echo $e->getMessage();
}

まずtryで文を実行し、異常放出異常throw new Exception/throw new ErrorException
再スナップ異常catch、出力異常
2、マニュアル例入れ子の異常
try {            try {                throw new MyException ( 'foo!' );            } catch ( MyException $e ) {                /* rethrow it */                throw $e ;            }        } catch ( Exception $e ) {             var_dump ( $e -> getMessage ()); }Exceptionでなければなりません.MyExceptionはエラーを報告します.