做个简单的例子:
@$conn = new mysqli("localhost","root","123456","luchg");
if(mysqli_connect_errno()){
throw new Exception("Always throw this error");
return true;
}else{
mysqli_query($conn,"set names 'utf8'");
return $conn;
}
在可能出异常的地方我们可以用“@”来抑制异常的发生,然后再后面判断异常信息,如果不抑制的话肯定会先报错,后面的的判断就没法执行到
如果有异常,new Exception() 并且抛出去, 在调用的地方捕获并处理异常:
try{
@$conn = $conManager -> getConn();
@$result = mysqli_query($conn,$sql);
}catch(Exception $e){
//再次进行异常的处理,如果不想代码继续执行,就调用exit()停止php脚本
}