views:

129

answers:

2

catch is not working because there is installed an exception handler using set_exception_handler()

I need "catch" to work, so I guess I need to unset the exception handler somehow. Things such as set_exception_handler(NULL) isn't working.

Any ideas how to unset the exception handler?

function my_exception_handler($exception) {
    error_log("caught exception: " . $exception->getMessage() );
}

set_exception_handler("my_exception_handler");

// QUESTION: how does on unset it ?
//set_exception_handler(NULL);

try {
    throw new Exception('hello world');
    error_log("should not happen");
} catch(Exception $e) {
    error_log("should happen: " . $e->getMessage());
}

Actual output:

caught exception: hello world

Desired output:

should happen: hello world

+3  A: 

restore_exception_handler, which is linked from the manual entry for set_exception_handler.

BTW, these exception handlers should only come into play when an exception is uncaught. A catch block should always have precedence.


Reading a little bit in the comments on the Exceptions page brings you to this bug and this bug. They describe exactly what you experience, Exceptions can't be caught when a custom error handler is defined.

Solution:

Fixed in 5.3 and HEAD, won't be backported to 5.2.

deceze
Inserting restore_exception_handler() doesn't solve it. Now I'm seeing a PHP Fatal error: Uncaught exception 'Exception' with message 'hello world' in /long/path/to/phpfile. How do I catch this exception?
neoneye
Is the code you posted the only code that is running? When I run that code, it works fine.
webbiedave
I'm using TYPO3 which installs its own exception handler and error handler. However I need temporarily bypass these handlers so "catch" will work, so that I can determine if there are problems when merging two PDF files. Here I need throw / catch to work. Otherwise I need to modify TCPDF.
neoneye
I'm using PHP version 5.2.3 on Mac OS X 10.6.
neoneye
@neoneye: Why do you believe that set_exception_handler overrides catches? That is not its behavior. I believe your problem lies elsewhere.
webbiedave
What are your typo3 config regarding exception and error handling ?
Arkh
@Arkh, I'm not at work now so I can't tell you what's in localconf.php nor in php_info.
neoneye
@webbiedave, I don't know PHP's exception system very well. Usually catch, catches what is thrown, so I was quite surprised that this wasn't the case in my PHP setup. set_exception_handler() sounded like a function that could be causing this. If you have better ideas what is causing this then let me know. Thanks.
neoneye
@deceze Looking at the bug links you have posted. Hmm. I find it hard to believe that exceptions all together isn't working for me in PHP, somebody would have noticed that catch wasn't working. There must be something else wrong in php.ini or somewhere else.
neoneye
@neoneye Have you tried it outside of TYPO3 and are getting the same results?
deceze
yeah in the http://stackoverflow.com/questions/2719584/catch-isnt-working I exercise the catch code in total isolation. It's just an index.php and I open it with Firefox, so no typo3 specific code is run.
neoneye
@neoneye Recompile and reinstall, then. Nuke from orbit to make sure. ;-)
deceze
Problem is that my code needs to run on the customer's typo3 installation, so nuking it is not an option. I guess I will have to run php in a subshell and then parse the output for fatal errors. I had hoped to get catch working so I wouldn't have to do this evil hack. I appreciate all your help.
neoneye
Solved. I'm now using popen and doing a preg_match afterwards to detect if it crashed.
neoneye
A: 

The function is restore_exception_handler. However, the handler should only be called when an exception is unhandled. It does not disable catches.

webbiedave
This doesn't solve my particular problem, see the replies to @deceze post.
neoneye