Page 1 of 1

Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 4:10 pm
by RobertGonzalez
This sounds weird to me but I have to ask...

Can a PHP app that throws an uncaught exception still process and output even though an exception was raised and thrown and written to the error log?

I ask this because today I finally figured out what was causing another problem I was having (namely multiple orphaned mysql instances). I realized that something was opening a connection to my database server but was never closing it out. This was happening to the tune of 9 to 20 connections per server request. What I found in my investigation was that my RewriteRules were not behaving as expected and requests that were being made for files and directories, though I told the server to skip them, were being routed through my frameworks bootstrap file.

Upon more investigation I found that requests for a JS (for example) were being made to http://mysite.com/Layout/Default/Default.js and were resulting in 404 errors within my framework. To the framework this meant that instead of serving up a requested page the app would instead serve up the doPageNotFound() found method of my Index controller and would attempt to parse the PageNotFound.php view.

However I forgot to actually put the view in the proper path so this view was throwing a View_Exception in my framework. However, this exception never halted execution of the main script. So everything ran hunky dory like nothing was wrong.

But something was wrong. The component controllers were dying on each request. And Rewrite handling was not working, so every source file for a given page request (whether it was a PHP component controller in the app or a CSS file from the view) was resulting in a 404, an open DB connection, an uncaught thrown exception and an orphan database connection.

My question is, why would an uncaught exception in a component controller or any other piece of an application not halt output of the script? I am baffled by this and would love someone with more PHP knowledge than me to help me understand what the hell is going on with this.

Re: Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 4:33 pm
by Benjamin
I can't test this because I am reinstalling my dev box, but what would this do?

Code: Select all

 
<h1>Exception Test</h1>
<?php
echo '<h2>About to throw exception...</h2>';
throw new Exception('Thrown Exception');
echo '<h2>Exception Thrown</h2>';
?>
<h1>End Exception Test</h1>
 

Re: Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 5:15 pm
by Chris Corbyn
astions wrote:I can't test this because I am reinstalling my dev box, but what would this do?

Code: Select all

 
<h1>Exception Test</h1>
<?php
echo '<h2>About to throw exception...</h2>';
throw new Exception('Thrown Exception');
echo '<h2>Exception Thrown</h2>';
?>
<h1>End Exception Test</h1>
 
You'd see:

Code: Select all

<h1>Exception Test</h1>
<h2>About to throw exception...</h2>
... STACK TRACE ...
Everything after the exception is never reached.

Re: Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 5:18 pm
by Benjamin
That's what I figured. If I remember correctly an uncaught exception will throw a fatal error. Everah I can't imagine there is any code being executed after the fact.

EDIT: Although if it's in an include, it may just stop processing that specific file...

Re: Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 5:52 pm
by dbevfat
Uncaught exception is a fatal error, which means the program halts. The only way to execute some code after that is to register a shutdown function (http://www.php.net/manual/en/function.r ... nction.php). To be precise: that works on my php 5.2, can't say about other versions, but I'm pretty sure this would be the case. The problem with this method is that within the shutdown functions you can't be sure how much cleanup has already been done (which -- if any -- objects were already destroyed), and that the output is already sent to browser (this can cause problems with compressed content). There could be more issues; these two are off the top of my head.

regards

Re: Thrown exceptions not halting app

Posted: Wed Feb 13, 2008 6:22 pm
by RobertGonzalez
It is a little hard to explain what was happening, but I will give it a go...

My bootstrap file includes my frameworks base static class. It calls a static method that instantiates the front controller and calls the display method.

The display method kinda does all the magic, routing as needed to the appropriate page controller. All of this happens without a hitch. The hitch comes in to play when it comes to components (or partials as some call them).

My page controller sets some component controllers which, upon instantiation, do their thing and return their output into a view variable of the same name as the component controller. This is where the weirdness was being triggered.

Because of my rewrite rules JS files that were known to exist as files by the server were for some reason being not found as files (!-f) in the rewrite rule. By rule then, the call to that file was being pumped into my bootstrap, which by its nature includes the framework static base class and attempts to display the called page (can anyone say recursion?). The issue here was that the file that was being called through the bootstrap was not found by the app and the framework was returning a 404 not found respons which includes the output of the action doPageNotFound which parsed the pageNotFound view which was not there. The framework throws an exception when the view for a particular action is not found. This happened five times in one page request. But it never showed to the screen. I didn't see it until I noticed a Javascript error in Firebug which, upon investigation, showed that instead of including the JS file in the markup the page not found exception was there instead.

The weirder thing about all this is that the JS files, CSS files and everything else were there. The exceptions were written to the apache error logs but the page rendered without issue including all the necessary support files.

I cannot explain why this happened the way it did. I do know that things are fixed for the time being, but I think it is a rather hacky ends to the means. Apache does not seem to be wanting to work the way I think it should.

Does any of this make any sense to anyone?

Re: Thrown exceptions not halting app

Posted: Thu Feb 14, 2008 10:27 am
by Jenk
The only answer is to wrap your entire application into a try/catch block. I.e. catch at the highest possible level.

Code: Select all

<?php
try {
  // everything here..
  Application::getInstance()->run();
} catch (Exception $e) {
  echo $e->getStackTrace();
}
 
doSomethingImportantAfterException();
 
?>
or to use register_shutdown_function();

Re: Thrown exceptions not halting app

Posted: Thu Feb 14, 2008 12:15 pm
by RobertGonzalez
I will be looking more into this. It just seems like odd behavior to me.