Jump to content

Archived

This topic is now archived and is closed to further replies.

shivabharat

Exception Handling Levels - Have a Look

How do you rate this tutorial?  

5 members have voted

  1. 1. How do you rate this tutorial?

    • Array
      4


Recommended Posts

I see lot of postings in the forum related to the notices, warning and handling errors. I would like to share my views in dealing with this and I hope this helps you.

 

To begin with lets see what each one is all about

 

Notices – The names says it all, they really don’t mean anything serious just a notice for you to consider. Example undefined string.

 

Warnings – You ought to give a consideration to these. A warning can occur due to different reasons like trying to refer a file in your script when you don’t have one in that name.

 

Fatal Errors - Something to deal with immediately.

 

Parse Error -

When you put in something like this u get this error

<? <html> </html> ?> 

The php parser will not identify HTML tags and you get a parse error.

 

 

Difference Between Error and Warning

The difference between Warning and Error is a warning will not terminate the script were as a fatal error would.

 

You can set the “error reporting” level in you “php.ini” file

 

Examples:

;

;  - Show all errors, except for notices

;

;error_reporting = E_ALL & ~E_NOTICE

;

;  - Show only errors

;

;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

;

;  - Show all errors except for notices

;

error_reporting  =  E_ALL & ~E_NOTICE

 

Now you feel that you don’t have rights to change the ini file, well you can tackle them in your script, now lets see how it’s done.

 

List of errors that occur

 

Error Type What do they mean?

 

E_ERROR --------- Fatal runtime error

E_WARNING --------- Non-fatal runtime error

E_PARSE --------- Runtime parse error

E_NOTICE --------- Non-fatal runtime notice

E_CORE_ERROR --------- Fatal startup error

E_CORE_WARNING --------- Non-fatal startup error

E_COMPILE_ERROR --------- Fatal compile-time error

E_COMPILE_WARNING --------- Non-fatal compile-time error

E_USER_ERROR --------- User-triggered fatal error

E_USER_WARNING --------- User-triggered non-fatal error

E_USER_NOTICE --------- User-triggered notice

E_ALL --------- All of the above

 

 

Error_reporting

 

To avoid errors in PHP script use the error_reporting function

 

<? error_reporting(0); ?>

 

This will not report any error. Don’t use this.

 

<? error_reporting(E_ALL); ?>

Reports all errors

 

<? error_reporting(E_ERROR | E_WARNING | E_NOTICE);?>

 

Will show only warning, notice & errors.

 

<? Echo error_reporting() ?> 

Will show your current error reporting level.

 

 

Hope this basic tutorial on Error Handling helped you. :lol:

Share this post


Link to post
Share on other sites

Personally, I prefer to turn off errors completely from displaying to the screen with ini_set(), then log the appropriate/significant errors to a log txt file with a combination of error_log(), set_error_handler(), and error_reporting() instead.

 

<?php
   function errorHandle($number, $string, $file, $line) {
       error_log("Error($number) on line $line in file $file. The error was "$string"n", 3, "errors.txt");
   }
   ini_set('display_errors', 0);
   set_error_handler("errorHandle");
   error_reporting(2039);
?>

Share this post


Link to post
Share on other sites
Notices – The names says it all, they really don’t mean anything serious just a notice for you to consider. Example undefined string.

i'll have to disagree slightly with this approach. IMHO, any time you have any error of any sort, whether it be a notice or a parse error, it needs to be dealt with before you launch a site. notices are serious in the light that they are showing you areas in which your code is not up to standards. if you have a notice, you have an area of code where you are not using good coding practices. for instance, using the example given above, when you refer to a variable when it has not been declared yet, you get a notice (undefined string)... all you have to do to get rid of the notice is check to see whether the variable isset() or not. it's not hard to deal with, it just takes some time and commitment from the coder, and this is what separates the dedicated coders from the hobbyists.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.