Re: Re: ErrorException and set_exception_handler()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/12/11 Hannes Magnusson <hannes.magnusson@xxxxxxxxx>:
> On Fri, Dec 11, 2009 at 23:39, Randall Girard <randallgirard@xxxxxxxxxxx> wrote:
>> Okay, my mistake.
>>
>>
>> E_USER_WARNING and E_USER_NOTICE are supposed to continue execution,
>> correct?
>
> Correct.
>
>
>> Then  I will restate what I previously said. throwing ErrorException from an
>
> No no. Stop there. Exceptions are *totally* different from E_*.
>
>
>> ERROR_HANDLER(...) is useless iwht E_USER_WARNING and E_USER_NOTICE. Why?
>> Because when you throw the exception from the ERROR_HANDLER, presumably it
>> may NOT get caught in a try/catch block. Therefor the execution cannot
>> continue.
>
> Exactly. And that is the point.
> Why would you be turning E_USER_* into exceptions if you don't want to
> bail out of the current scope?
>
> If you throw an exception from the error handler, what exactly do you
> expect to happen?
> I gave several reasons for why that simply is impossible, but I am
> curious: what exactly are you expecting?
>
>
>
>> Therefor, I think that either a check to see if the TRY/CATCH has been
>> initiated further up the call stack, or a function to call from within the
>> EXCEPTION_HANDLER that will return execution to the previous scope.
>>
>>
>> I don't see how this is difficult to understand.
>
> So you expect a scripting language to scroll up the entire execution
> chain to see if there is a catch() block at all, and if there is no
> catch() block, the simply ignore the exception and continue?
> If thats your expectations then you are severely misunderstanding the
> concept of exceptions.
>
>
> Please do "reply-all". And please do not top-post.
>
>
> -Hannes
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

In support of Hannes, and hopefully providing a simpler script to show
what is happening.

-----------script------------
<?php
function uncaught_exception_handler(Exception $e) {
  echo 'Exception : ', $e->getMessage(), PHP_EOL;
}

function uncaught_error_handler($i_ErrNo, $s_Error, $s_File, $i_Line,
$a_Context) {
  echo 'A PHP error has been generated. It will be turned into an
exception.', PHP_EOL;
  switch($i_ErrNo) {
    case     0 : $s_ErrNo = '@'; return True; break;
    case     1 : $s_ErrNo = 'E_ERROR'; break;
    case     2 : $s_ErrNo = 'E_WARNING'; break;
    case     4 : $s_ErrNo = 'E_PARSE'; break;
    case     8 : $s_ErrNo = 'E_NOTICE'; break;
    case    16 : $s_ErrNo = 'E_CORE_ERROR'; break;
    case    32 : $s_ErrNo = 'E_CORE_WARNING'; break;
    case    64 : $s_ErrNo = 'E_COMPILE_ERROR'; break;
    case   128 : $s_ErrNo = 'E_COMPILE_WARNING'; break;
    case   256 : $s_ErrNo = 'E_USER_ERROR'; break;
    case   512 : $s_ErrNo = 'E_USER_NOTICE'; break;
    case  1024 : $s_ErrNo = 'E_USER_NOTICE'; break;
    case  2048 : $s_ErrNo = 'E_STRICT'; break;
    case  4096 : $s_ErrNo = 'E_RECOVERABLE_ERROR'; break;
    case  8192 : $s_ErrNo = 'E_DEPRECATED'; break;
    case 16384 : $s_ErrNo = 'E_USER_DEPRECATED'; break;
  }
  throw new Exception("PHP Error : $s_Error ($s_ErrNo) in $s_File at
Line $i_Line", $i_ErrNo);

  // Never reached as execution continues in the "catch" clause or by
a uncaught exception handler.
  echo 'The exception has been thrown.', PHP_EOL;
}

set_error_handler('uncaught_error_handler');
set_exception_handler('uncaught_exception_handler');

try {
	echo 'About to divide by zero inside a try/catch.', PHP_EOL;
	$a = 1 / 0;
} catch(Exception $e) {
	echo 'Caught : ', $e->getMessage(), PHP_EOL;
}

echo 'Caught divide by zero E_WARNING.', PHP_EOL, PHP_EOL;

echo 'About to divide by zero without a safety net.', PHP_EOL;
$b = 1 / 0;

// Never reached as we are not catching the execption thrown by the
uncaught error handler
echo 'Un-caught divide by zero E_WARNING.', PHP_EOL;
?>
-----------script------------

outputs ...

--------output---------
About to divide by zero inside a try/catch.
A PHP error has been generated. It will be turned into an exception.
Caught : PHP Error : Division by zero (E_WARNING) in Z:\test_error.php
at Line 36
Caught divide by zero E_WARNING.

About to divide by zero without a safety net.
A PHP error has been generated. It will be turned into an exception.
Exception : PHP Error : Division by zero (E_WARNING) in
Z:\test_error.php at Line 44
--------output---------


The whole issue is that you are using the (and I can't stress this
enough) _UNCAUGHT_ handlers.

These are the last ditch attempt / safety net for your code. It is
STILL your responsibility to pre-detect the potential errors and code
around them.

A try/catch block will be enough, as demonstrated - I successfully
capture a divide by zero.

Richard.
-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux