Does anyone know if it is possible to continue execution of the script somehow after exception is caught
with set_exception_handler() function.
Manual says it cannot be done.
...Execution will stop after the exception_handler is called.
Moderator: General Moderators
...Execution will stop after the exception_handler is called.
Jenk wrote:Post examples of your code, and what you are trying to achieve..
Code: Select all
set_exception_handler('catch_exception');
function catch_exception($t){
echo $t->getMessage();
}
class My_Exception extends Exception
{
public function __construct($sMessage,$iLevel = 0, $aOptions=false)
{
parent::__construct($sMessage, $iLevel);
$this->errorMessage = ($sMessage ? $sMessage : $this->getMessage());
$this->errorLevel = $iLevel; // our level
$this->errorOptions = $aOptions;
$this->errorCode = ($aOptions['type'] ? $aOptions['type'] : $this->getCode());
$this->errorFile = ($aOptions['file'] ? $aOptions['file'] : $this->getFile());
$this->errorLine = ($aOptions['line'] ? $aOptions['line'] : $this->getLine());
$this->errorTrace = ($aOptions['context'] ? $aOptions['context'] : $this->getTrace());
}
}
throw new My_Exception('exception msg');
echo "not executed";Code: Select all
<?php
function except_handle($e)
{
global $db;
$db->close();
echo 'Exception caught: ' . $e->getMessage() . "\n";
echo 'Database connection has been closed';
}
set_exception_handler('except_handle');
$db = new DataBaseClass();
$db->connect();
$db->doSomethingThatThrowsException(); //throws an exception.
?>Code: Select all
<?php
$db = new DataBaseClass();
$db->connect();
try {
$db->doSomethingThatThrowsException();
} catch (Exception $e) {
$db->close();
echo 'Database Exception caught: ' $e->getMessage() . "\n";
echo "Database connection has been closed!\n";
$db->setStatus('disconnected');
echo 'Continuing without Database connectivity!';
}
//rest of page..
?>Code: Select all
try {
throw new My_Exception('exception msg');
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}