574Part IIIAdvanced Features and TechniquesLet s look at (Web hosting script) the
Thursday, August 16th, 2007574Part IIIAdvanced Features and TechniquesLet s look at the example covered in Listing 31-3 and consider using custom exceptions. Peoplesigning in using this code may forget to include the usr prefix in their user name, so if it smissing you might want to try adding it and validating again rather than immediately haltingthe program. Listing 31-3:Recovering using custom exceptions // include the file which will validate the user IDrequire_once( includes/usr_functions.php ); // retrieve the user ID to validate$user_id = $_POST[ user_id ]; try { // set the display message based on whether or not // user ID is validif (!is_valid_user($user_id)) { $msg = Sorry, $user_id is not a valid user ID. ; } else { $msg = $user_id is a valid user ID. ; } } catch(PrefixException $ex) { // if prefix is missing, try again with proper prefix$user_pre = usr . $user_pre; if (!is_valid_user($user_pre)) { // second attempt has failed, retrieve message$msg = ($ex->getMessage()); } } catch(LengthException $ex) { // retrieve the message from the exception object$msg = ($ex->getMessage()); } //define custom exception classesclass PrefixException extends Exception { function __construct($message) { parent::Exception($message); } } class LengthException extends Exception { function __construct($message) {
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.