578Part IIIAdvanced Features and TechniquesDefining (Top web site) an error handlerThere s
578Part IIIAdvanced Features and TechniquesDefining an error handlerThere s an important question to ask at this point: What information do you want displayed tothe user when an error occurs? Usually, it s not important, or even preferred, to display detailsof the inner workings of your application to an end user; not to mention that errors look uglyon a Web page. By creating a function that designs a custom error message, then setting thatfunction as the default error handler, you can avoid the awkward and unprofessional displayof errors to a user. First, let s create a function and determine what information we would like to provide. We willneed to accept as input parameters the error type, message, filename, and line number. function error_msg($err_type, $err_msg, $err_file, $err_line) ( echo
; echo We re sorry, but an error has occurred . in this page. ; echo Please access the Help . page, ; echo or try again later. ; echo
; echo
; } ?> We have elected, in this case, to provide information on the specific error and where itoccurred. Depending on the situation, we may want to provide very little information to theuser other than the fact that an error has occurred and provide information on what to trynext. Now that we have defined our custom error handler, we simply need to refer to the functionwithin the code using the set_error_handler()function: set_error_handler( error_msg ); With this code in place, all errors that are enabled by the error-reporting level will directthrough our custom function, with the unfortunate exception of a fatal error. See Figure 31-3for an example of such an error being displayed.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.