- PHP 5 is that you can now use exceptions, like other OOP
languages.
- PHP 5 introduces an exceptions objects to simplify your
error .
- An
Exception
is any unusual condition that
causes an alteration in program flow, such as a missing file or a
null pointer.
- There are different error handling methods:
- Basic use of Exceptions.
- Creating a custom exception handler.
- Multiple exceptions.
- Re-throwing an exception Setting a top level exception
handler.
Use of Exceptions :
- When an exception is thrown,so that code will not be
executed, and will try to find the matching "catch" block.
- If an exception is not caught, a fatal error will be issued
with an "Uncaught Exception" message.
Example :
<?php
//create function with an exception
function check($num)
{
if($num>10)
{
throw new Exception("Value must be 10 or below");
}
return true;
}
//trigger exception
check(11);
?>
Output :
Fatal error: Uncaught exception 'Exception' with message 'Value must be 10 or below' in C:\xampp\htdocs\First\exception.php:7 Stack trace: #0>
C:\xampp\htdocs\First\exception.php(13):check(11) #1 {main} thrown
Try, throw and catch
- Try :try is used to define a block of code in which
exception can be occurred.
- Catch :catch block immediately after try block. The
catch block will tell your program just what to do in case the
exception actually does occur. If no exception occurs, then the
catch block is ignored, and your program moves along smoothly.
- Throw :to throw an exception explicitly.
Example :
<?php
//create function with an exception
function check($num)
{
if($num>10)
{
throw new Exception("Value must be 10 or below");
}
return true;
}
// exception in a "try" block
try
{
check(10);
//If the exception is thrown,
echo 'If you see this, the number is 10 or below';
}
//catch exception
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
Output :
If you see this, the number is 10 or below
Creating a Custom Exception Class
- We can create own exceptions by extending 'Exception'
class.
- Own exception is also known as "User Defined Exception".
Example :
<?php
class customException extends Exception
{
public function errorDisplay()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is invalid E-Mail address';
return $errorMsg;
}
}
$email = "abc@example..com";
try
{
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is invalid
throw new customException($email);
}
}
catch (customException $e)
{
//display custom message
echo $e->errorDisplay();
}
?>
Output :
Error on line 21 in C:\xampp\htdocs\First\customException.php: abc@example..com is invalid E-Mail address
Multiple Catch
- A single try block can have multiple catch blocks.
- It's required when the try block has statements that
generates different types of exceptions.
- If the first catch block contains the Exception class
object then the subsequent catch blocks are never executed.
- The last catch block in multiple catch blocks must contain
the Exception class object.
- Because the java complier gives an error saying that the
subsequent catch blocks haven't been reached.
Example :
<?php
class customException extends Exception
{
public function errorDisplay()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': '.$this->getMessage().' is not a valid E-Mail address';
return $errorMsg;
}
}
$email = "abc@example.com";
try
{
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is invalid
throw new customException($email);
}
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
throw new Exception("$email is e-mail");
}
}
catch (customException $e)
{
echo $e->errorDisplay();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
Output :
abc@example.com is e-mail
Re-throwing Exceptions
- If you use throw an exception more than one time within a
"catch" block.
Example :
<?php
class customException extends Exception
{
public function errorDisplay()
{
//error dispkay
$errorMsg = $this->getMessage().' is invalid E-Mail address.';
return $errorMsg;
}
}
$email = "abc@example.com";
try
{
try
{
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
//throw exception if email is invalid
throw new Exception($email);
}
}
catch(Exception $e)
{
//re-throw exception
throw new customException($email);
}
}
catch (customException $e)
{
//display custom message
echo $e->errorDisplay();
}
?>
Output :
abc@example.com is invalid E-Mail address.