Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't catch an Exception

Status
Not open for further replies.

SimonSellick

Programmer
Nov 3, 2003
305
GB
...or possibly, none is being thrown.

I have a script that runs OK under Apache but fails to complete under IIS. It's embedded within an <IMG> tag so generates an image rather than text.

I've wrapped it in a simple try {...} catch {(Exception $e)...} block, but whilst this catches an exception if I deliberately raise one in the script, it doesn't catch whatever is stopping the script from completing. Is there something else that I need to do to catch PHP-raised exceptions, or does PHP not raise an exception for some run-time errors?

Any help appreciated.
 
parse errors and fatal errors do not get caught by customised error handlers and similar. the (avowed) reason is that either type of error would mean that php was likely to be in an "unstable" state and so the best thing to do is a graceful bail out. this is paraphrased from a post by Zev Suraski.

another reason may be that you have different values for display_errors in your php.ini file on the two boxes.
 
Thanks for the response jpadie,

Might a reference to a non-existent element of an array be treated as a fatal error on one box but not the other (depending on display_errors)?

Simon
 
nope. the behaviour would be consistent. display_errors just turns on and off the display of errors (!).

but a different value of error_reporting (either in the code or in php.ini) would give a different response between the two.

i'm not convinced about using exceptions as error handlers. i guess you could do it (for non-fatal errors) but i can't really see why you'd want to. the thing about exceptions is you have to catch every variant to make it worthwhile (imo).

there is an ok article on them here and here

if you want to use exceptions to catch errors then (i think) you will need to change the error handler.

if you would like help debugging your script on the IIS box the please post the script and the problem here. I'm sure we can help.
 
jpadie,

Thanks for the info and sorry for the long delay in replying - I was exploring another avenue. I've found the questionable bit of code and fixed it (as I suspected, a reference to an undefined array element) so the problem has gone away but I will try to find out the error_reporting config setting on the client's server in case that explains the difference between the two platforms.

Thanks too for the links to the Exceptions articles. From using them in other languages (Jave, VB) I have to admit to being a big fan of exceptions, mainly because they permit you to code to the expected data / flow structure whilst giving the control to trap likely problems where you want to handle them and still leaving a catch-all to handle anything unexpected gracefully. Compared with the amount of return-handling code needed in C and assembler, it seems to me to give a dramatic improvement in clarity and code simplicity. I agree with you that you have to catch them all somewhere, but being able to choose different places for different exceptions is a great help.

Anyway, thanks again for the suggestions.

Simon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top