//create function with an exception
function checkNum($number){
if($number>1){
throw new Exception("Value must be 1 or below");
}
}
//trigger exception
checkNum(2);
上面的代码会获得类似这样的一个错误:
Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in C:\webfolder\test.php:6
Stack trace: #0 C:\webfolder\test.php(12):checkNum(28) #1 {main} thrown in C:\webfolder\test.php on line 6
注意:PHP默认是警告模式,如果需要对系统错误使用异常处理机制,则要在处理代码之前设置错误处理模式
set_error_handler(function(){
throw new Exception('错误!');
});
class customException extends Exception{
public function errorMessage(){
return 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
}
}
$email = "someone@example...com";
try{
//使用PHP过滤器验证邮箱有效性
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
throw new customException($email);
}
}catch (customException $e){
echo $e->errorMessage();
}
class customException extends Exception{
public function errorMessage(){
return = 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
}
}
$email = "someone@example.com";
try{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
throw new customException($email);
}
//check 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();
}
上面的代码测试了两种条件,如何任何条件不成立,则抛出一个异常:
执行 "try" 代码块,在第一个条件下,不会抛出异常。由于 e-mail 含有字符串 "example",第二个条件会触发异常。"catch" 代码块会捕获异常,并显示恰当的错误消息,如果没有捕获 customException,只捕获了 base exception,则在那里处理异常。
//create function with an exception
function checkNum($number){
if($number>1){
throw new Exception("Value must be 1 or below");
}
}
//trigger exception
checkNum(2);
上面的代码会获得类似这样的一个错误:
Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in C:\webfolder\test.php:6
Stack trace: #0 C:\webfolder\test.php(12):checkNum(28) #1 {main} thrown in C:\webfolder\test.php on line 6
注意:PHP默认是警告模式,如果需要对系统错误使用异常处理机制,则要在处理代码之前设置错误处理模式
set_error_handler(function(){
throw new Exception('错误!');
});
class customException extends Exception{
public function errorMessage(){
return 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
}
}
$email = "someone@example...com";
try{
//使用PHP过滤器验证邮箱有效性
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
throw new customException($email);
}
}catch (customException $e){
echo $e->errorMessage();
}
class customException extends Exception{
public function errorMessage(){
return = 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
}
}
$email = "someone@example.com";
try{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
throw new customException($email);
}
//check 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();
}
上面的代码测试了两种条件,如何任何条件不成立,则抛出一个异常:
执行 "try" 代码块,在第一个条件下,不会抛出异常。由于 e-mail 含有字符串 "example",第二个条件会触发异常。"catch" 代码块会捕获异常,并显示恰当的错误消息,如果没有捕获 customException,只捕获了 base exception,则在那里处理异常。