Re: Throwing an exception seems to defeat output buffering
| From: | Ondrej Kulaty | Date: | Tue, 03 Feb 2009 08:22:49 +0000 |
| Subject: | Re: Throwing an exception seems to defeat output buffering | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287561@lists.php.net to get a copy of this message | ||
Output buffer is flushed at the end of script. When you throw that exception
in try block, this command: exit( 'Contents: ' . ob_get_clean()); never
executes and it continues to catch block where you are outputing exception
message, and it is added to the buffer, then the script ends and buffer
flushes.
=======================
"Leif Wickland" <leifwickland@gmail.com> pí¹e v diskusním pøíspìvku
news:c5b9ee2c0902022202v6e2a071emfb062aa868ed7fa8@mail.gmail.com...
>I would expect that if I turn on output buffering, echo something,
> throw an exception,
> and catch the exception, nothing will have been actually output. That
> doesn't seem
> to be the case. Throwing an exception seems to defeat output buffering.
>
> In the following code, I would not expect to see the <h1>, but I do.
>
>
>
> <?
> try {
> ob_start();
> echo '<h1>You should not see this!</h1>';
> throw new Exception('<h2>This should be the first output.</h2>');
> exit( 'Contents: ' . ob_get_clean());
> }
> catch (Exception $ex) {
> exit('<h2>Exception:</h2>' . $ex->getMessage());
> }
>
>
>
> I'm exercising that code on PHP 5.2.4 and 5.2.8.
>
> Does anybody know why throwing an exception seems to override
> ob_start(), flushing the buffered output? Is there a workaround?
>
> Thank you,
>
> Leif Wickland