throw new Exception() 和 throw_exception() 有什么区别?
来源:6-9 完成文章排序功能
宇xixi
2017-01-23
// 代码1-模型层
if(!$newsId || !is_numeric($newsId)){
throw new Exception("Id不符合规则");
}
if(!$listorderId || !is_numeric($listorderId)){
throw new Exception("listorderId不符合规则");
}
//代码2-模型层
if(!$newsId || !is_numeric($newsId)){
throw_exception("Id不符合规则");
}
if(!$listorderId || !is_numeric($listorderId)){
throw_exception("listorderId不符合规则");
}
//控制器层
try{
if(!$listorder || !is_array($listorder)){
return show(0,"排序值出现错误");
}else{
foreach($listorder as $newsId => $value){
$res = D("News")->updateListorderById($newsId,$value);
if($res === false){
$error[] = $newsId;
}
}
}
}catch(Exception $e){
return show(0,$e->getMessage());
}当使用listorder故意输入不为数字时
●模型层使用throw new Exception()抛出异常,控制器层可以捕获到抛出的异常,并执行catch的内容
●模型层使用throw_exception()抛出异常,控制器无法捕获,会直接返回相应的结构。如下图:

所以想请问下两者有什么区别?
写回答
1回答
-
zjx2017
2017-04-25
一个是抛出异常类Exception的对象,即手动抛出异常.
一个是新建一个异常类Exception的对象,但并不抛出.00
相似问题