hrtehnolog Posted October 12, 2013 Share Posted October 12, 2013 Hi! I was playing with a book example, and I don't understand why it prints error message twice. I have changed code a bit. Commented echo under catch (Invalid_Email_Exception $e), and put echo under constructor. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php /* The Invalid_Email_Exception class is responsible for notifying the site administrator in the case that the e-mail is deemed invalid. */ class Invalid_Email_Exception extends Exception { function __construct($message, $email) { $this->message = $message; $this->notifyAdmin($email); echo $message; echo "<br>"; } private function notifyAdmin($email) { mail("admin@example.org", "INVALID EMAIL", $email, "From:web@example.com"); } } /* The Subscribe class is responsible for validating an e-mail address and adding the user e-mail address to the database. */ class Subscribe { function validateEmail($email) { try { if ($email == "") { throw new Exception("You must enter an e-mail address!"); } else { list($user, $domain) = explode("@", $email); if (!checkdnsrr($domain, "MX")) throw new Invalid_Email_Exception( "Invalid e-mail address!", $email); else return 1; } } catch (Exception $e) { echo $e->getMessage(); } catch (Invalid_Email_Exception $e) { //echo $e->getMessage(); } } /* This method would presumably add the user's e-mail address to a database. */ function subscribeUser() { echo $this->email . " added to the database!"; } } #end Subscribe class /* Assume that the e-mail address came from a subscription form. */ $_POST['email'] = "wrongEmailArdess"; /* Attempt to validate and add address to database. */ if (isset($_POST['email'])) { $subscribe = new Subscribe(); if ($subscribe->validateEmail($_POST['email'])) $subscribe->subscribeUser($_POST['email']); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/282919-prints-error-message-twice/ Share on other sites More sharing options...
Ch0cu3r Posted October 12, 2013 Share Posted October 12, 2013 (edited) You're getting the error message twice because you're echo'ing the error message when you throw the error (in the constructor) and when you catch the error. When you throw the error throw new Invalid_Email_Exception("Invalid e-mail address!", $email); This will call the __constructor method for the Invalid_Email_Exception class class Invalid_Email_Exception extends Exception { function __construct($message, $email) { $this->message = $message; $this->notifyAdmin($email); echo $message; echo "<br>"; } ... On line 15 you set the error. On 17 you echo the error. The error is caught in the try catch block and echo'd again. Edited October 12, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282919-prints-error-message-twice/#findComment-1453685 Share on other sites More sharing options...
hrtehnolog Posted October 12, 2013 Author Share Posted October 12, 2013 Thanks for answering. Still confused In the catch part, line 47, echo is commented. For example if I modify constructor to this: function __construct($message, $email) { $this->message = $message; $this->notifyAdmin($email); echo $message; echo "<br>"; echo "Testing"; echo "<br>"; I get this: Invalid e-mail address!TestingInvalid e-mail address! Why is "Testing" not printed twice? Or, if I uncomment echo on line 47 of first post, I get the same result: Invalid e-mail address! TestingInvalid e-mail address! Does it mean that "catch" automatically prints error, or runs constructor once more? Quote Link to comment https://forums.phpfreaks.com/topic/282919-prints-error-message-twice/#findComment-1453686 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 12, 2013 Solution Share Posted October 12, 2013 (edited) No the constructor is only called when you throw an error. To see when the __constructor is called, change it to this function __construct($message, $lineNumb, $email) { $this->message = $message; $this->notifyAdmin($email); echo "<br>" . __CLASS__ . ' was called on line <b>' . $lineNumb . '</b> and set the following error - <b>' . $message . '</b>'; echo "<br>"; } Now change throw new Invalid_Email_Exception("Invalid e-mail address!", $email); to throw new Invalid_Email_Exception("Invalid e-mail address!", __LINE__, $email); Run your code and you should get the following output Invalid_Email_Exception was called on line 40 and set the following error - Invalid e-mail address! Invalid e-mail address! I think the order for the catch() blocks need to be reversed, so check for the Invalid_Email_Exception first followed by checking for Exception } catch (Invalid_Email_Exception $e) { echo $e->getMessage(); } catch (Exception $e) { echo $e->getMessage(); } Edited October 12, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282919-prints-error-message-twice/#findComment-1453688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.