Try Catch Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Try Catch Error

Post by neridaj »

Hey Everyone,

I don't understand why I'm getting an error with the opening brace in the try block of this code, it's straight from "PHP and MySQL Web Development". If you see something I don't see I would like to know what it is.

code:

Code: Select all

<?php
  // include function files for this application
  require_once('bookmark_fns.php');

  //create short variable names
  $email=$_POST['email'];
  $username=$_POST['username'];
  $passwd=$_POST['passwd'];
  $passwd2=$_POST['passwd2'];
  // start session which may be needed later
  // start it now because it must go before headers
  session_start();
  try
  {
    // check forms filled in
    if (!filled_out($_POST))
    {
      throw new Exception('You have not filled the form out correctly - please go back'
          .' and try again.');    
    }
   
    // email address not valid
    if (!valid_email($email))
    {
      throw new Exception('That is not a valid email address.  Please go back '
                          .' and try again.');
    } 

    // passwords not the same 
    if ($passwd != $passwd2)
    {
      throw new Exception('The passwords you entered do not match - please go back'
                           .' and try again.');
    }

    // check password length is ok
    // ok if username truncates, but passwords will get
    // munged if they are too long.
    if (strlen($passwd)<6 || strlen($passwd) >16)
    {
      throw new Exception('Your password must be between 6 and 16 characters.'
                           .'Please go back and try again.');
    }
   
    // attempt to register
    // this function can also throw an exception
    register($username, $email, $passwd);
    // register session variable 
    $_SESSION['valid_user'] = $username;
    
    // provide link to members page
    do_html_header('Registration successful');
    echo 'Your registration was successful.  Go to the members page '
          .'to start setting up your bookmarks!';
    do_html_url('member.php', 'Go to members page');
   
   // end page
   do_html_footer();
  }
  catch (Exception $e)
  {
     do_html_header('Problem:');
     echo $e->getMessage(); 
     do_html_footer();
     exit;
  } 
?>
Thanks,

J

Jcart | Please put your php code in tags[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

What is your PHP version?
There are 10 types of people in this world, those who understand binary and those who don't
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Post by neridaj »

PHP version is 4.4.7
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Error Handling in PHP 4
Up to version 4, developers had to use the 'set_error_handler' callback function to implement some sort of generic error handling. This is where Exceptions in PHP5 come into play. Through the use of exceptions, you can do some very nice error reporting, sending stacktraces to yourself and display some meaningfull message to the user.
Switch to PHP5 :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply