标签:
异常处理主要是依靠try{ throw error}catch(error){....}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 面向过程 -->
<?php
$_POST[‘name‘]=‘‘;
try{
if($_POST[‘name‘]==‘‘){
throw new Exception("姓名为空");
}
}
catch(Exception $error){
echo $error->getMessage();
}
?>
<!-- 函数处理 -->
<?php
function check($name){
if($name!=‘‘){
return true;
} else {
throw new Exception("姓名为空");
}
}
try{
check($_POST[‘name‘]);
}
catch(Exception $error){
echo $error->getMessage();
}
?>
<!-- 面向对象 -->
<?php
class Person{
private $name;
public function __construct($name){
if($name!=‘‘){
$this->name=$name;
} else {
throw new Exception("姓名为空");
}
}
}
try{
$per=new Person($_POST[‘name‘]);
}
catch(Exception $error){
echo $error->getMessage();
}
?>
</body>
</html>
总结:必须有try,而且必须有抛出异常,然后才能针对抛出的异常进行处理
标签:
原文地址:http://www.cnblogs.com/-beyond/p/5697159.html