Jump to content

prints error message twice?


hrtehnolog
Go to solution Solved by Ch0cu3r,

Recommended Posts

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>
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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!
Testing
Invalid 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!

Testing
Invalid e-mail address!

 

 

Does it mean that "catch" automatically prints error, or runs constructor once more?

Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.