php5->set_exception_handler-> stops execution of scrip

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

php5->set_exception_handler-> stops execution of scrip

Post by jmut »

http://bg.php.net/manual/en/function.se ... andler.php

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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Post examples of your code, and what you are trying to achieve..
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Jenk wrote:Post examples of your code, and what you are trying to achieve..

I am somehow trying to make an automation system that does not use try catch non stop but rather handle stuff automatically.
But as I see I should not use exception in all cases but rather gracefully finish!! the script in case exception is not cought.
All other casses (like debugging or just showing user friendly msgs) I will make another class with unique codes or something for this.....that is how
execution of script will not be stopped.


Generaly speaking, trying to make system that automatically catched php_errors, smarty errors, exceptions apperantly not...
not automagically deciding appropriate outcome :)
Also implementing dB,file,mail debugging/logging functionality.

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";
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That doesn't quite work out, because the exception handler is used to finalise any loose ends your application/system may have, such as I/O interfaces that will require closing to free up resources etc. and for logging the error, or displaying the error.

Such as:

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.

?>
With the try/catch method, the idea is to localise all 'loose end tieing' so that you don't have a huge block of closes etc. in your exception handler. This is primarily for ease of maintenance, both in initial development (so you don't forget to add bits to the exception handler) and also in later development (the exception is handled immediately after the excpetion is thrown)

ala:

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..

?>
Hope this helps you to understand. :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Oh, and just to add - the Try/Catch method does not exit the script, which is nice. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Unfortunately set_exception_handler() was a bad name choice. I should have been set_uncaught_exception_handler(). Like Jenk says, you should do:

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";
}
I would also note that the power of exceptions is not to replace if() structures inline for error checking (especially in PHP). Where exceptions become useful is when a lower level throws an exception and a higher (calling) layer catches it. The reason is because it allows you to remove direct dependencies on things like return values and let you abstract the error handling process.
(#10850)
Post Reply