Tag Archives: Error

set_exception_handler

It sets a user-defined function to handle exception

Example

<?php
function customException($exception){
echo  $exception->getMessage();
}
set_exception_handler(‘customException’);
throw new Exception(‘Uncaught Exception occurred’);
?>

Output

Exception: Uncaught Exception occurred

set_error_handler

It sets a user-defined function to handle errors

Example

<?php
function errorFunc($errno, $errstr, $errfile, $errline) {
echo $errstr.” on ” . $errfile .” at “. $errline;
}
set_error_handler(“errorFunc”);
$val=5;
if ($test>1)  trigger_error(“An error occured”);
?>

Output

Undefined variable: test on /var/www/test/index.php at 7

restore_exception_handler

It r restores the previous exception handler

Example

<?php

function customError($errno, $errstr, $errfile, $errline)  {
echo $errno.””.$errstr.” , File name – “.$errfile.” at line No”.$errline;
}

set_error_handler(“customError”);

$val=5;

if ($val>1)  trigger_error(“An error occured”);

restore_error_handler();

if ($val>5)  trigger_error(“An error occured”);

?>

Output

exception_handler_1 – This triggers the first exception handler…