Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > PHP Forum & Tutorial


PHP Exception Handling


Reply
Views: 3529  
Thread Tools Rate Thread
  #6  
Old 05-04-2009, 05:35 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
Multiple Exceptions

It is possible for a script to use multiple exceptions to check for multiple conditions.


It is possible to use several if..else blocks, a switch, or nest multiple exceptions. These exceptions can use different exception classes and return different error messages:






class customException extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': '.$this->getMessage().' is not a valid E-Mail address';
return $errorMsg;
}
}

$email = "someone@example.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");
}
}

catch (customException $e)
{
echo $e->errorMessage();
} catch(Exception $e)
{
echo $e->getMessage();
}
?>


Example explained:

The code above tests two conditions and throws an exception if any of the conditions are not met:
  1. The customException() class is created as an extension of the old exception class. This way it inherits all methods and properties from the old exception class
  2. The errorMessage() function is created. This function returns an error message if an e-mail address is invalid
  3. The $email variable is set to a string that is a valid e-mail address, but contains the string "example"
  4. The "try" block is executed and an exception is not thrown on the first condition
  5. The second condition triggers an exception since the e-mail contains the string "example"
  6. The "catch" block catches the exception and displays the correct error message
If there was no customException catch, only the base exception catch, the exception would be handled there
Reply With Quote
  #7  
Old 05-04-2009, 05:36 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
Re-throwing Exceptions

Sometimes, when an exception is thrown, you may wish to handle it differently than the standard way. It is possible to throw an exception a second time within a "catch" block.


A script should hide system errors from users. System errors may be important for the coder, but is of no interest to the user. To make things easier for the user you can re-throw the exception with a user friendly message:




class customException extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = $this->getMessage().' is not a valid E-Mail address.';
return $errorMsg;
}
} $email = "someone@example.com"; try
{
try
{
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
//throw exception if email is not valid
throw new Exception($email);
}
}
catch(Exception $e)
{
//re-throw exception
throw new customException($email);
}
} catch (customException $e)
{
//display custom message
echo $e->errorMessage();
}
?>


Example explained:

The code above tests if the email-address contains the string "example" in it, if it does, the exception is re-thrown:
  1. The customException() class is created as an extension of the old exception class. This way it inherits all methods and properties from the old exception class
  2. The errorMessage() function is created. This function returns an error message if an e-mail address is invalid
  3. The $email variable is set to a string that is a valid e-mail address, but contains the string "example"
  4. The "try" block contains another "try" block to make it possible to re-throw the exception
  5. The exception is triggered since the e-mail contains the string "example"
  6. The "catch" block catches the exception and re-throws a "customException"
  7. The "customException" is caught and displays an error message
If the exception is not caught in its current "try" block, it will search for a catch block on "higher levels".
Reply With Quote
  #8  
Old 05-04-2009, 05:38 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
Set a Top Level Exception Handler

The set_exception_handler() function sets a user-defined function to handle all uncaught exceptions.





function myException($exception)
{
echo "Exception: " , $exception->getMessage();
} set_exception_handler('myException'); throw new Exception('Uncaught Exception occurred');
?>



The output of the code above should be something like this:




Exception: Uncaught Exception occurred



In the code above there was no "catch" block. Instead, the top level exception handler triggered. This function should be used to catch uncaught exceptions.

Rules for exceptions

  • Code may be surrounded in a try block, to help catch potential exceptions
  • Each try block or "throw" 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) in a catch block within a try block
A simple rule: If you throw something, you have to catch it.
Reply With Quote
Reply

New topics in PHP Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)