470,833 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Problems with set_error_handler() :/

Hi !
It's my first post here: Hello everyone :-)
Do you know why this code doesnt work ? Thanks for any help.

<?
echo('start<br>');
// redefine the user error constants - PHP 4 only
define("FATAL", E_USER_ERROR);
define("ERROR", E_USER_WARNING);
define("WARNING", E_USER_NOTICE);

// set the error reporting level for this script
error_reporting(FATAL | ERROR | WARNING);
echo('test<br>');
function catch_err($errno, $errstr,$errfile, $errline)
{
echo "<b>FATAL</b> [$errno] $errstr<br />\n";
echo('error!');
}
set_error_handler("catch_err", E_ERROR);

trigger_error("test...", E_USER_ERROR);
echo('<br>end');
?>

--
zYm3N[@interia.pl]

..:: C++ | C | PHP | HTML | Delphi | Pascal
..:: >> http://zymen.cjb.net <<
..:: http://zymen.cjb.net/cytowanie.html
Jul 17 '05 #1
7 5063
On Sun, 1 Aug 2004 14:43:07 +0200, zYm3N <zy***@interia.pl> wrote:
Hi !
It's my first post here: Hello everyone :-)
Do you know why this code doesnt work ? Thanks for any help.

<?
echo('start<br>');
// redefine the user error constants - PHP 4 only
define("FATAL", E_USER_ERROR);
define("ERROR", E_USER_WARNING);
define("WARNING", E_USER_NOTICE);

// set the error reporting level for this script
error_reporting(FATAL | ERROR | WARNING);
You've just turne off all the usual PHP errors, and only set it to use
user-level errors. It's pretty clear why you've done this, but it's masked a
problem a bit further down...
echo('test<br>');
function catch_err($errno, $errstr,$errfile, $errline)
{
echo "<b>FATAL</b> [$errno] $errstr<br />\n";
echo('error!');
}
set_error_handler("catch_err", E_ERROR);
After commenting out the error_reporting call further up, it reveals the
actual problem:

Warning: Wrong parameter count for set_error_handler() in
/home/andyh/public_html/test.php on line 16

See:
http://uk2.php.net/manual/en/functio...or-handler.php

You haven't said what version of PHP you're on, but the second argument only
exists in PHP5, not PHP4.

Because of this it probably hasn't installed the error handler.
trigger_error("test...", E_USER_ERROR);
echo('<br>end');
?>


After changing the set_error_handler line to set_error_handler("catch_err") I
get:

start
test
FATAL [256] test...
error!
end

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2
Dnia Sun, 01 Aug 2004 13:58:52 +0100, Andy Hassall spłodził(a):
http://uk2.php.net/manual/en/functio...or-handler.php

I read it once again.. And I found something interesting:

"Note: The following error types cannot be handled with a user defined
function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
E_COMPILE_WARNING, and E_STRICT."

Now my code looks like:
<?
echo('start<br>');
error_reporting(E_ALL);
echo($_SERVER["SERVER_SOFTWARE"]);

function catch_error($errno, $errstr, $errfile, $errline)
{
echo('<br>error<br>');
}

set_error_handler('catch_error');

trigger_error("Cannot divide by zero", E_USER_ERROR); //there my function
works fine..

$a->out("it can't work"); //there is an error too!,
//and now I know, why isn't it catch by my function..
echo('start<br>');
?>

The code above produces:
"
start
Apache/1.3.29 (Win32) mod_gzip/1.3.26.1a PHP/4.3.4
error

Fatal error: Call to a member function on a non-object in
c:\usr\apache\httpd\html\p\error.php on line 15
"

Thanks for help.. Now it is a bit easier for me.

--
zYm3N[@interia.pl]

..:: C++ | C | PHP | HTML | Delphi | Pascal
..:: >> http://zymen.cjb.net <<
..:: http://zymen.cjb.net/cytowanie.html
Jul 17 '05 #3
Andy Hassall wrote:
Hi !
It's my first post here: Hello everyone :-)
Do you know why this code doesnt work ? Thanks for any help.

<?
echo('start<br>');
// redefine the user error constants - PHP 4 only
define("FATAL", E_USER_ERROR);
define("ERROR", E_USER_WARNING);
define("WARNING", E_USER_NOTICE);

// set the error reporting level for this script
error_reporting(FATAL | ERROR | WARNING);


If i may add a comment to what Andy just said:

Don't use parentheses for echo and print. As it is not a function, those
aren't needed, and you'll save yourself a good deal of pain when you'll
be adding more than one argument to what you're printing/echoing on screen.

Regards,
Sebastian
Jul 17 '05 #4
Dnia Sun, 01 Aug 2004 15:36:44 +0200, Sebastian Lauwers spłodził(a):
Don't use parentheses for echo and print. As it is not a function, those
aren't needed, and you'll save yourself a good deal of pain when you'll
be adding more than one argument to what you're printing/echoing on screen.


Can You give me an example ? I dont understand, what you really mean.
--
zYm3N[@interia.pl]

..:: C++ | C | PHP | HTML | Delphi | Pascal
..:: >> http://zymen.cjb.net <<
..:: http://zymen.cjb.net/cytowanie.html
Jul 17 '05 #5
Look at this article for another approach -
http://www.tonymarston.co.uk/php-mys...orhandler.html

--
Tony Marston

http://www.tonymarston.net
"zYm3N" <zy***@interia.pl> wrote in message
news:6i***************************@40tude.net...
Dnia Sun, 01 Aug 2004 13:58:52 +0100, Andy Hassall spłodził(a):
http://uk2.php.net/manual/en/functio...or-handler.php I read it once again.. And I found something interesting:

"Note: The following error types cannot be handled with a user defined
function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
E_COMPILE_WARNING, and E_STRICT."

Now my code looks like:
<?
echo('start<br>');
error_reporting(E_ALL);
echo($_SERVER["SERVER_SOFTWARE"]);

function catch_error($errno, $errstr, $errfile, $errline)
{
echo('<br>error<br>');
}

set_error_handler('catch_error');

trigger_error("Cannot divide by zero", E_USER_ERROR); //there my function
works fine..

$a->out("it can't work"); //there is an error too!,
//and now I know, why isn't it catch by my

function.. echo('start<br>');
?>

The code above produces:
"
start
Apache/1.3.29 (Win32) mod_gzip/1.3.26.1a PHP/4.3.4
error

Fatal error: Call to a member function on a non-object in
c:\usr\apache\httpd\html\p\error.php on line 15
"

Thanks for help.. Now it is a bit easier for me.

--
zYm3N[@interia.pl]

.:: C++ | C | PHP | HTML | Delphi | Pascal
.:: >> http://zymen.cjb.net <<
.:: http://zymen.cjb.net/cytowanie.html

Jul 17 '05 #6
On Sun, 1 Aug 2004 15:48:19 +0200, zYm3N <zy***@interia.pl> wrote:

Can You give me an example ? I dont understand, what you really mean.


echo 'No parenthesis needed for this error!';

Jul 17 '05 #7
zYm3N <zy***@interia.pl> wrote in message news:<6i***************************@40tude.net>...
http://uk2.php.net/manual/en/functio...or-handler.php I read it once again.. And I found something interesting:

"Note: The following error types cannot be handled with a user defined
function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
E_COMPILE_WARNING, and E_STRICT."


You can catch E_ERROR (fatal) errors with output buffering
<http://www.google.com/search?q=cache:ru2.php.net/set_error_handler#35622>

To catch all errors you may want to try
<http://www.phpclasses.org/logwatcher> as they use JavaScript
solutions.
Now my code looks like:
<?
echo('start<br>');
error_reporting(E_ALL);


AFAIK, error_reporting() will have no effects if custom error
handler is enabled.

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by JohnVT | last post: by
5 posts views Thread by lawrence | last post: by
14 posts views Thread by Jim Hubbard | last post: by
5 posts views Thread by Corky | last post: by
reply views Thread by Sergistm | last post: by
3 posts views Thread by Taras_96 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.