Exception
Handling
A system to
send an error message in runtime from the place an error has occurred to the
place a method get called.
PHP provides
a built-in class Exception to send
such messages.
Use throw
keyword to throw an instance of some exception.
Use try-catch block to trap such kind of exceptions using getMessage() method of Exception class.
One try can
have many catch statements
Example 1
Create a
class Maths with a static function as factorial() which takes a number and
returns factorial of that number. If number is below zero then issue an
exception with a message “Sorry! Number must be >=0”
<?php
class Maths
{
static function factorial($n)
{
if($n<0) throw
new Exception("Sorry! Number must be >=0");
if($n==0)
return 1;
else
return
$n*Maths::factorial($n-1);
}
}
try
{
$f=Maths::factorial(-6);
echo
$f;
}
catch(Exception $ex)
{
echo "Error :
".$ex->getMessage();
}
?>
Example 2
Create a
class Customer having data members as cid, name and balance.
Create a
constructor to initialize data in data members. If deposit amount is less than
500 then say “Sorry! Min amount to open account is 500”
Create
required setter and getters
Create a
function deposit() to deposit some amount. If amount less then equal to 0 then
issue a message “Sorry! Invalid Amount”
Create a
function withdraw() to withdraw some amount. If amount is more than balance then
issue a message “Sorry! Insufficient funds.. cannot withdraw”
<?php
class
Customer
{
var $cid,$name,$balance;
function
Customer($cid,$name,$amount)
{
if ($amount<500)
throw new Exception("Sorry! Min amount must be 500");
$this->cid=$cid;
$this->name=$name;
$this->balance=$amount;
}
function getCid()
{
return $this->cid;
}
function getName()
{
return $this->name;
}
function getBalance()
{
return
$this->balance;
}
function deposit($amount)
{
if($amount<=0) throw
new Exception("Sorry! Invalid Amount");
$this->balance=$this->balance+$amount;
}
function withdraw($amount)
{
if($amount>$this->balance)
throw new Exception("Sorry! Insufficient Balance... unable to
withdraw");
$this->balance=$this->balance-$amount;
}
}
?>
<?php
try
{
$c=new Customer(1234,"Amit
Verma",4000);
$c->deposit(3000);
$c->withdraw(1500);
echo "Balance of
".$c->getName()." is ".$c->getBalance();
}
catch(Exception
$ex)
{
echo "Error :
".$ex->getMessage();
}
?>
Creating
custom exceptions
We can
inherit one class into another class using extends keyword. The functions of parent class now
becomes functions of child class.
Create a
class and inherit that class with Exception class to define your own function
to send the error message.
<?php
class InvalidNumberException extends
Exception
{
public function getErrorMessage()
{
return "Sorry! Number must be
>=0";
}
}
class Maths
{
static function factorial($n)
{
if($n<0) throw new
InvalidNumberException();
if($n==0)
return 1;
else
return
$n*Maths::factorial($n-1);
}
}
try
{
$f=Maths::factorial(6);
echo $f;
}
catch(InvalidNumberException $ex) //known
error
{
echo "Error : ".
$ex->getErrorMessage();
}
catch(Exception $ex) //unknown error
{
echo "Error :
".$ex->getMessage();
}
?>
Setting a
custom exception handler function
We can also
define some function to automatically handle the exception without need of try
and catch
Use set_exception_handler() function to
define the function name to handle the exceptions
<?php
function myException($ex)
{
echo "<b>Error :</b>
" . $ex->getMessage();
}
set_exception_handler('myException');
class Maths
{
static function factorial($n)
{
if($n<0) throw new Exception("Sorry! Number must be
>=0");
if($n==0)
return 1;
else
return
$n*Maths::factorial($n-1);
}
}
$f=Maths::factorial(-6);
echo
$f;
?>
No comments:
Post a Comment