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 会报错