You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

<?php
class emailException extends Exception{
function __toString()
{
return "email is null ".$this->getFile().' '.$this->getLine().'<br>';
}
}
class nameException extends Exception{}
function reg($reg){
if (empty($reg['email'])) { //如果数组reg里键email的值是空的
throw new emailException("email is null",1);//2.抛出异常后推出了函数
}
echo "-----".'<br>'; //代码没有执行到这里
if (empty($reg['name'])) {
throw new nameException('name is null',2);
}
echo "-----".'<br>';
}
try {
$reg = array('phone'=>'12344544');
reg($reg); //1.传入的数组里没有eamil和name
} catch (emailException $e) { //3.捕获的是email is null这个异常
echo $e;
} catch (nameException $e) { //代码没有进入这个catch
echo $e->getMessage().'<br>';
echo $e->getCode().'<br>';
} finally { //4.最后进入finally
echo "finally";
}
?>