View Full Version : error handling
Alphamonkey
05-14-2007, 02:54 AM
Im fairly new to PHP and i was wondering, how do i make it so that instead of there being the regular PHP error that PHP will give
Warning: blah: blah blah blah
it gives something i say like
Oops! An error occured.
is this possible for every error ever? or do i have to somehow tell it to know if a certain error occured...and if so how and if not so....how.
:thumbsup:
gunman
05-14-2007, 08:47 AM
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.
/* Intentional file error */
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
For more information check php documentation Error Control Operators
aedrin
05-14-2007, 04:44 PM
@ should not be used except in extreme cases where situations prevent you from dealing with an error.
All you need to use is: set_error_handler()
If you want to not show anything else when an error occurs, you need to use output buffering.
Then in your error handler you would clear the buffer and send out your message instead.
Alphamonkey
05-17-2007, 05:01 AM
ok thank you very much that helped alot
printf
05-17-2007, 11:31 AM
Error handling is the key to sound application development. What ever you can control in your script should be controlled there. And @ -> suppression is never ever a good idea. As aedrin, said you can control just about anything that errors for PHP 4. As of PHP 5 full error handling has become refined, now you can handle any error without the need of primitive hacks that you once may have needed in PHP 4.
<?php
// php 5
try
{
// try to open the the file and return a file array
$file = file ( './path/file.txt' );
}
catch ( Exception $e )
{
// file() function failed, try something else, or throw the exception
echo "Oh no, exception: " . $e->getMessage () . "\r\n";
}
?>
Alphamonkey
05-17-2007, 09:48 PM
Error handling is the key to sound application development. What ever you can control in your script should be controlled there. And @ -> suppression is never ever a good idea. As aedrin, said you can control just about anything that errors for PHP 4. As of PHP 5 full error handling has become refined, now you can handle any error without the need of primitive hacks that you once may have needed in PHP 4.
<?php
// php 5
try
{
// try to open the the file and return a file array
$file = file ( './path/file.txt' );
}
catch ( Exception $e )
{
// file() function failed, try something else, or throw the exception
echo "Oh no, exception: " . $e->getMessage () . "\r\n";
}
?>
This was more of what i was looking for, i have experiecnce with Try and Catch in VB :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.