SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Clearwater, FL
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    try/catch question

    Hi all... I have been lurking around this board for quite some time now and decided it was time to post a question.

    If I have a class with a method that calls another method that throws an exception should the first method use a try/catch block even if it does not handle the exception. For example...

    PHP Code:
    abstract class database
      
    {
      abstract protected function 
    _connect($host,$user,$password);

      function 
    connect($host,$user,$password)
        {
        if (
    strlen($host) == 0)
          {
          throw new 
    Exception('Blank host name');
          }
        elseif (
    strlen($user) == 0)
          {
          throw new 
    Exception('Blank user name');
          }
        elseif (
    strlen($password) == 0)
          {
          throw new 
    Exception('Blank password');
          }
        else
          {
          
    $this->LinkID $this->_connect($host,$user,$password);
          return (
    $this->LinkID !== false);
          }
        }
      }

    class 
    mysql_database extends database
      
    {
      protected function 
    _connect($host,$user,$password)
        {
        if (
    $LinkID = @mysql_connect($host,$user,$password))
          {
          return 
    $LinkID;
          }
        else
          {
          throw new 
    Exception("Unable to connect to $host");
          }
        }
      }


    myDB = new mysql_database();

    try
      {
       
    myDB->connect('mysql_server','root','very secure password');
      }
    catch (
    Exception $e)
      {
      print 
    "Exception: ".$e->getMessage();
      } 
    Or this...

    PHP Code:
    abstract class database
      
    {
      abstract protected function 
    _connect($host,$user,$password);

      function 
    connect($host,$user,$password)
        {
        if (
    strlen($host) == 0)
          {
          throw new 
    Exception('Blank host name');
          }
        elseif (
    strlen($user) == 0)
          {
          throw new 
    Exception('Blank user name');
          }
        elseif (
    strlen($password) == 0)
          {
          throw new 
    Exception('Blank password');
          }
        else
          {
          try
            {
            
    $this->LinkID $this->_connect($host,$user,$password);
            return (
    $this->LinkID !== false);
            }
          catch(
    Exception $e)
            {
            throw 
    $e;
            }
          }
        }
      }

    class 
    mysql_database extends database
      
    {
      protected function 
    _connect($host,$user,$password)
        {
        if (
    $LinkID = @mysql_connect($host,$user,$password))
          {
          return 
    $LinkID;
          }
        else
          {
          throw new 
    Exception("Unable to connect to $host");
          }
        }
      }


    myDB = new mysql_database();

    try
      {
       
    myDB->connect('mysql_server','root','very secure password');
      }
    catch (
    Exception $e)
      {
      print 
    "Exception: ".$e->getMessage();
      } 
    The only difference is the try/catch in database::connect() method. Functionality wise it does not appear to be any different one way or the other but the second version provides some self documentation that the statements in the try block may throw an exception.

    Does anybody know of any reason to use one over the other?

    - Mark

  2. #2
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zippo
    Does anybody know of any reason to use one over the other?

    - Mark
    I'd use the first. The second holds unneeded code. In java normally you have a throws clause in the function prototype. In PHP the next best thing is to put a @throws in your DocComments imo.

    Becareful with Exceptions though, as they will make many implict exit points for functions/methods

    For instance

    PHP Code:
    $this->LinkID $this->_connect($host,$user,$password); 
    is an implict exit point (as the throw in _connect might cause things to exit if you do things like the first example. These implict exit points can be a big pitfall if you have resurces that need to be deallocated automaticlly when a varible goes out of scope. This is not too much of an issue in PHP, and I can't see any scenarios where this might be a problem. However if you are a C++ programmer, you will be well aware of this problem if you have done things like multithreaded programming as you may realise the Exceptions make releasing locked resources a nightmare. In C++ this is easy to get round as destructors get called automatically, so you can write "guard classes". Guard classes are allocated on the stack, but their resource they guard can be a pointer, which is allocated in construction and deallocated in destruction, thus automatically destroying the resource when it goes out of scope. I would have no idea how you would implement this in PHP5 due to the destructor querk (coz they need to be manually involked with unset()).

    -- Jason

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, it would help everyone if try/catch and throw actually appeared in the PHP manual. (Last time I looked -- earlier this week -- they did not.)

    (Look to the left. Apparently, I'm a Sitepoint enthusiast. Considering that I rarely post, how on earth did I manage that?)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •