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、手冊例子 嵌套的異常
ry {try {
throw new MyException ( 'foo!' );
} catch ( MyException $e ) {
/* rethrow it */
throw $e;
}
} catch ( Exception $e ) {
var_dump ( $e -> getMessage ());
}
必須是 Exception ,MyException會報錯