PHP 5 has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling.
Try − A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".
Throw − This is how you trigger an exception. Each "throw" must have at least one "catch".
Catch − A "catch" block retrieves an exception and creates an object containing the exception information.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
Exceptions can be thrown (or re-thrown) within a catch block.
There are following functions which can be used from Exception class.
getMessage() − message of exception
getCode() − code of exception
getFile() − source filename
getLine() − source line
getTrace() − n array of the backtrace()
getTraceAsString() − formated string of trace
Standard Exception Handling
//create function with an exception
function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
try{
checkNum(2);
}
catch(Exception $e){
echo $e->getMessage();
}
Custom Exception Handling
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@mail.com";
try {
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
//throw exception if email is not valid
throw new customException($email);
}
//check for "example" in mail address
if(strpos($email, "example") !== FALSE) {
throw new Exception("$email is an example e-mail");
}
//check for "test" in mail address
if(strpos($email, "test") !== FALSE) {
throw new Exception("$email is an test e-mail");
}
}
catch (customException $e) {
echo $e->errorMessage();
}
catch(Exception $e) {
echo $e->getMessage();
}
Try − A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".
Throw − This is how you trigger an exception. Each "throw" must have at least one "catch".
Catch − A "catch" block retrieves an exception and creates an object containing the exception information.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
Exceptions can be thrown (or re-thrown) within a catch block.
There are following functions which can be used from Exception class.
getMessage() − message of exception
getCode() − code of exception
getFile() − source filename
getLine() − source line
getTrace() − n array of the backtrace()
getTraceAsString() − formated string of trace
Standard Exception Handling
//create function with an exception
function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
try{
checkNum(2);
}
catch(Exception $e){
echo $e->getMessage();
}
Custom Exception Handling
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@mail.com";
try {
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
//throw exception if email is not valid
throw new customException($email);
}
//check for "example" in mail address
if(strpos($email, "example") !== FALSE) {
throw new Exception("$email is an example e-mail");
}
//check for "test" in mail address
if(strpos($email, "test") !== FALSE) {
throw new Exception("$email is an test e-mail");
}
}
catch (customException $e) {
echo $e->errorMessage();
}
catch(Exception $e) {
echo $e->getMessage();
}