Can someone please show me a real world example of using this custom exception handling class? [url]http://php.net/manual/en/language.exceptions.extending.php
The example they use is not making sense to me because they are creating a test class with all kinds of extra parameters and constructs in it. And then the examples test the test class instead of testing a try catch block on a real piece of code.
I need a real basic example like if I want to put a function in a try catch that trys to make 2 = 3 and I want the custom handler to return the message “ya big dummy! 2 can’t equal 3!”.
I don’t see them using the “throw new” key words anywhere in the examples and another confusing thing is the customFunction(). What is is for and what goes inside it?
Thanks for any help in understanding this.
[code=php]
<?php
/**
* Class we'll use to generate an exception
*/
class Foo {
public function bar($a, $b)
{
if($a != $b) {
throw new FooException("$a does not equal $b.");
}
echo "Yes, $a equals $b.";
}
}
/**
* Class to extend Exception so we can use a custom method
*/
class FooException extends Exception
{
public function myCustomMethod()
{
echo "<p class='error'>Hey, dummy! " . $this->message . "!</p>n";
}
}
/**
* Our custom exception-handler
*/
function myExceptionHandler($exception)
{
if(is_a($exception, 'FooException')) {
$exception->myCustomMethod();
}
else {
echo "<p class='error'>Standard Error: " . $exception->message . "</p>n";
}
}
/**
* Put the exception-handler into effect
*/
set_exception_handler('myExceptionHandler');
/**
* Let's see what happens
*/
$test = new Foo();
$test->bar(3, 3);
$test->bar(2, 3);
[/code]
<i>
</i>Yes, 3 equals 3.
Hey, dummy! 2 does not equal 3.!
[CODE]
<?php
try {
if(2 != 3){
throw new Exception("ya big dummy! 2 can't equal 3!");
}
} catch (Exception $e) {
echo "Caught my exceptionn", $e;
}
?>
[/CODE]
[CODE]
<?php
//testing db exception handling
require_once('class_db.php');
set_exception_handler('dbExceptionHandler');
$dbcon = new db();
$dbcon->init_con();
?>
[/CODE]
[CODE]
<?php
require_once('../includes/config.inc.php');
include('class_ehandler_db.php');
class db extends ehandler_db{
function dbExceptionHandler($exception){
if(is_a($exception, 'ehandler_db')){
$exception->customFunction();
}
else{
echo "<p class='error'>Standard Error: " . $exception->message . "</p>n";
}
}
function init_con(){
//initiate the connection
//using constants from config file
try{
$con = mysql_connect('DB_HOST', 'DB_USER', 'DB_PASS');
if(!$con){
throw new ehandler_db("Unable to connect to database");
}
else{
echo "Connection Successful";
}
}
catch (Exception $e){
echo "Exception Caught: " . $e->getMessage();
}
}
?>
[/CODE]
[CODE]
<?php
/*overrides standard exceptions. Requires php v 5.3.0 +
Code provided by: php.net (http://php.net/manual/en/language.exceptions.extending.php)
Modified by: Ian Simmons
Date: 08-03-11
*/
class ehandler_db extends Exception{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0, Exception $previous = null) {
// some code
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}n";
}
public function customFunction() {
echo "<p class='error'>Hey, dummy! " . $this->message . "!</p>n";
}
}
?>
[/CODE]
[code=php]
<?php
abstract class CustomException extends Exception
{
/**
* This method must be defined in each sub-class
* @return string Message to be output by exception handler
*/
public abstract function custom();
/**
* The callback function for set_exception_handler()
*/
public static function handler($e) {
if(is_a($e, 'CustomException')) {
user_error($e->custom());
}
else {
user_error($e->message);
}
}
}
[/code]
[code=php]
<?php
require_once('path/to/CustomException.php');
/**
* The exception class for our DB class
*/
class DBException extends CustomException
{
public function custom()
{
return ("Hey! You can't do that! Stop it!<br />nERROR: " . $this->message);
}
}
[/code]
[code=php]
<?php
require_once('path/to/CustomException.php');
/**
* The DB class
*/
class DB extends mysqli
{
public function testException()
{
throw new DBException("This is a test.");
}
}
[/code]
[code=php]
<?php
require_once('path/to/DB.php'); // will include our Exception classes, too, in this case
// notice use of array() to specify class::method as the callback function:
set_exception_handler(array('CustomException', 'handler'));
$test = new DB('localhost', '####', '####');
$test->testException();
[/code]
[CODE]
try{
$con = mysql_connect(DB_HOST, 'DB_USER', DB_PASS);
if(!$con){
throw new Exception("Unable to connect to database. This is a test.");
}
else{
echo "Connection Successful";
}
}
catch (Exception $e){
echo "Exception Caught: " . $e->getMessage();
}
[/CODE]
[CODE]
public abstract function custom();
[/CODE]
[CODE]
class ehandler_db extends customException{
public function custom()
{
return ("Hey! You can't do that! Stop it!<br />nERROR: " . $this->message);
}
}
[/CODE]
[CODE]
<?php
abstract class customException extends Exception{
/**
* This method must be defined in each sub-class
* @return string Message to be output by exception handler
*/
//comented this line out and it made no difference
//tried putting some code in it and get error
public abstract function custom();
/**
* The callback function for set_exception_handler()
*/
public static function handler($e) {
if(is_a($e, 'customException')) {
user_error($e->custom());
}
else {
user_error($e->message);
}
}
}
?>
[/CODE]
[CODE]
<?php
require_once('class_customException.php');
class ehandler_db extends customException{
public function custom()
{
return ("Hey! You can't do that! Stop it!<br />nERROR: " . $this->message);
}
}
?>
[/CODE]
[CODE]
<?php
include('../includes/config.inc.php');
require_once('class_ehandler_db.php');
class db{
function init_con(){
//initiate the connection
//using constants from config file
try{
$con = mysql_connect(DB_HOST, 'DB_USER', DB_PASS);
if(!$con){
throw new ehandler_db("Unable to connect to database. This is a test.");
}
else{
echo "Connection Successful";
}
}
catch (Exception $e){
echo "Exception Caught: " . $e->getMessage();
}
}
}
?>
[/CODE]
[CODE]
<?php
//testing db exception handling
//error_reporting(0);
require_once('class_db.php');
set_exception_handler(array('customException', 'handler'));
$dbcon = new db();
$dbcon->init_con();
?>
[/CODE]
[CODE]
class DBException extends CustomException
{
public function custom()
{
return ("Hey! You can't do that! Stop it!<br />nERROR: " . $this->message);
}
}
[/CODE]
[CODE]
<?php
require_once('class_db.php');
$sql = new db();
$sql->init_con();
?>
[/CODE]
[CODE]
<?php
require_once('class_eHandler_db.php');
error_reporting(0);
class db{
function init_con(){
try {
$con = mysql_connect('localhost', 'user', 'pass');
if(!$con){
throw new eHandler_db(eHandler_db::THROW_CUSTOM);
}
} catch (customException $e) { // Will be caught
echo "Caught db connection exceptionn", $e;
$e->customFunction();
}
}
}
?>
[/CODE]
[CODE]
<?php
/**
* Create a class to test the exception
*/
require_once('class_customException.php');
class eHandler_db{
public $var;
const THROW_NONE = 0;
const THROW_CUSTOM = 1;
const THROW_DEFAULT = 2;
function __construct($avalue = self::THROW_NONE) {
switch ($avalue) {
case self::THROW_CUSTOM:
// throw custom exception
throw new customException('custom message about db connection', 5);
break;
case self::THROW_DEFAULT:
// throw default one.
throw new Exception('default message', 6);
break;
default:
// No exception, object will be created.
$this->var = $avalue;
break;
}
}
}
?>
[/CODE]
[CODE]
<?php
/**
* Define a custom exception class
*/
class customException extends Exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0, Exception $previous = null) {
// some code
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}n";
}
public function customFunction() {
echo "<p>An error has occured and a message is being sent to the site administrator</p>";
//code here to collect and send message
}
}
?>
[/CODE]
[CODE]
Caught db connection exception customException: [5]: custom message about db connection
An error has occured and a message is being sent to the site administrator
[/CODE]
[CODE]
main script index.php in this case
/*****************************************************************************/
<?php
require_once('classes/class_db.php');
$sql = new db();
$sql->init_con();
?>
database object class_db.php
/*****************************************************************************/
<?php
require_once('class_dbException.php');
error_reporting(0);
class db{
function init_con(){
try {
$con = mysql_connect('localhost', 'user', 'pass');
if(!$con){
throw new dbException('Could not connect with the specified data', 5);
}
} catch(ac_customException $e){
echo "Caught db connection exceptionn", $e;
$e->dbNotify($e);
}
}
}
?>
custom database exception class class_dbException.php
/*******************************************************************************/
<?php
require_once('ac_customException.php');
class dbException extends ac_customException{
/*db exception special functions below this line****************************/
public function dbNotify($e){
//dbNotify emails web master with exception info
$host = $_SERVER['SERVER_NAME'];
$remoteIP = $_SERVER['REMOTE_ADDR'];
//testing but in reality would use code to email the info to the web master
//and log to specified log file
echo "<p>Caught db connection exception -- " . $e . "</p>";
echo "<p>From remote IP " . $remoteIP . " on server: " . $host . "</p>";
}
}
?>
abstract custom exception class ac_customException.php
/***************************************************************************/
<?php
abstract class ac_customException extends Exception{
public function __construct($message, $code = 0, Exception $previous = null) {
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}n";
}
/*list abstract classes for use by sub classes below this line*********/
abstract public function dbNotify($e); //notify web master
abstract public function navNotify($e); //notify web master
}
[/CODE]
0.1.9 — BETA 5.6