hnlixf
乞丐
UID 73674
精华
0
积分 -56
帖子 14
金钱 -56 喜悦币
威望 0
人脉 0
阅读权限 1
注册 2006-5-19
状态 离线
|
异常处理--为什么会有这样的结果?
<?php
class customException extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
return $errorMsg;
}
}
$email = "someone@example.com";
try
{
/ eck if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is not valid
throw new customException($email);
}
/ eck for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
throw new Exception("$email is an example e-mail");
}
}
catch (customException $e)
{
echo $e->errorMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
正确的结果是: someone@example.com is an example e-mail
为什么两个只是执行了第一个try,另一个没的执行?按道理第一个条件也会成立的呀?
|
|