playing catch in PHP

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I'm trying to catch all of the errors I generate with PHP but don't really know the best way to do and how exactly to accomplish what I want.

I want to be able to throw exceptions, catch them, produce an error stack and kill the script on the last one. Don't know how to come by all this... don't really want to work on a different class right now until I get this done so I won't have to go back and edit all the error handlings in the classes.

Thank you!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Nevermind... all I had to do was get my hands dirty on the subject. I figured it out. Thanks :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I got a different sort of question on this. Now that I sort of figured it out, I have a different question that I kind of know an answer to but don't know how to implement it.

Here is my question: I have the try/catch system set up for connecting to the server and database and the way I have error handling set up right now is that they could be silenced and not show up at all... which is fine at the moment since I don't have anything being put into a database or retrieved from a database.

Or I could get all the errors to show up (I guess I could extend on error levels to make some not show up and some to show up). This is all just a preamble to the question :)

Would it be a good idea to do something like the following:

page1.php
PHP Code: [ Select ]
// Retrieving some data from a database
try
{
    // Getting our SQL build
    $sql = $db->build_query(array('*','`database`.`table`', "field = 'value'", 'value2 ASC'));
   
    // Retrieving data set from the database
    $data = $db->fetch_rowset($sql);
}
 
// Catching any exceptions thrown
catch( exception $e )
{
    // Dealing with our error here
    $err->_error($e, ERROR_SYSTEM, LOG_FILE_SQL);
}
  1. // Retrieving some data from a database
  2. try
  3. {
  4.     // Getting our SQL build
  5.     $sql = $db->build_query(array('*','`database`.`table`', "field = 'value'", 'value2 ASC'));
  6.    
  7.     // Retrieving data set from the database
  8.     $data = $db->fetch_rowset($sql);
  9. }
  10.  
  11. // Catching any exceptions thrown
  12. catch( exception $e )
  13. {
  14.     // Dealing with our error here
  15.     $err->_error($e, ERROR_SYSTEM, LOG_FILE_SQL);
  16. }

mysql.php (mysql class... few example functions)
PHP Code: [ Select ]
public function fetch_rowset($sql, $type = 'assoc')
{
    // Creating a result resource
    try
    {
        $results = $this->rresource($sql, true);
    }
   
    catch (exception $e)
    {
        throw $e;
       
        return false;
    }
   
    // Retrieving the data
    while($row = mysql_fetch_assoc($results))
    {
        $result[] = $row;
    }
       
    // Returning the result
    return $result;
}
 
public function rresource($sql = null, $return = false)
{
    // Checking if we are connected to MySQL
    if(!is_resource($this->mysql_link))
    {
        $this->connect();
    }
   
    // Checking if the SQL is empty
    if(is_null($sql))
    {
        $sql = $this->get_last_sql();  
    }
 
    // Getting the resource into a variable
    $resource = mysql_query($sql);
   
    // Setting the last result variable
    $this->last_result = $resource;
 
    // Checking if the resource was created properly
    if(is_resource($resource) || $resource === true)
    {
        // Checking if we are returning the result
        if($return === true)
        {
            return $resource;
        }
       
        // Otherwise we are returning true
        return true;
    }
    else
    {
        // Throwing an exception
        throw new exception("Error setting MySQL Resource. SQL used: '{$sql}' MySQL Error: " . mysql_error());
       
        // It was a failure... return false
        return false;
    }
}
  1. public function fetch_rowset($sql, $type = 'assoc')
  2. {
  3.     // Creating a result resource
  4.     try
  5.     {
  6.         $results = $this->rresource($sql, true);
  7.     }
  8.    
  9.     catch (exception $e)
  10.     {
  11.         throw $e;
  12.        
  13.         return false;
  14.     }
  15.    
  16.     // Retrieving the data
  17.     while($row = mysql_fetch_assoc($results))
  18.     {
  19.         $result[] = $row;
  20.     }
  21.        
  22.     // Returning the result
  23.     return $result;
  24. }
  25.  
  26. public function rresource($sql = null, $return = false)
  27. {
  28.     // Checking if we are connected to MySQL
  29.     if(!is_resource($this->mysql_link))
  30.     {
  31.         $this->connect();
  32.     }
  33.    
  34.     // Checking if the SQL is empty
  35.     if(is_null($sql))
  36.     {
  37.         $sql = $this->get_last_sql();  
  38.     }
  39.  
  40.     // Getting the resource into a variable
  41.     $resource = mysql_query($sql);
  42.    
  43.     // Setting the last result variable
  44.     $this->last_result = $resource;
  45.  
  46.     // Checking if the resource was created properly
  47.     if(is_resource($resource) || $resource === true)
  48.     {
  49.         // Checking if we are returning the result
  50.         if($return === true)
  51.         {
  52.             return $resource;
  53.         }
  54.        
  55.         // Otherwise we are returning true
  56.         return true;
  57.     }
  58.     else
  59.     {
  60.         // Throwing an exception
  61.         throw new exception("Error setting MySQL Resource. SQL used: '{$sql}' MySQL Error: " . mysql_error());
  62.        
  63.         // It was a failure... return false
  64.         return false;
  65.     }
  66. }

Here is how I implemented the whole try / catch system into my scripts, but I'm not sure if I did it right and I'm not exactly sure if it works with these situations. Here is my _error(); function which I call on page1.php
PHP Code: [ Select ]
public function _error($e, $error_type, $file, $message = null)
{
    // Checking to see what kind of an error it is
    if(is_object($e))
    {
        // Checking if we are to display the error (no matter the graceful error or not)
        if($error_type === 0 || $error_type === 2)
        {
            // It over-rides graceful errors
            $this->display_exception($e, $message);
           
            // Killing the script
            exit;
        }
        else
        {
            // It does not over-ride graceful errors
            if(!GRACEFUL_ERRORS || ADMIN_DEBUG)
            {
                // We are to display the error
                $this->display_exception($e, $message);
            }
        }
    }
    else
    {
        // Checking if we are to display the error (no matter the graceful error or not)
        if($error_type === 0 || $error_type === 2)
        {
            // It over-rides graceful errors
            $this->display_error($e, $message);
           
            // Killing the script
            exit;
        }
        else
        {
            // It does not over-ride graceful errors
            if(!GRACEFUL_ERRORS || ADMIN_DEBUG)
            {
                // We are to display the error
                $this->display_error($e, $message);
            }
        }
    }
   
    // Logging if we need to
    $this->_log($e, $file);
   
   
    // Checking if we are to kill the script or not
    if(DIE_ON_ERR)
    {
        exit;
    }
}
  1. public function _error($e, $error_type, $file, $message = null)
  2. {
  3.     // Checking to see what kind of an error it is
  4.     if(is_object($e))
  5.     {
  6.         // Checking if we are to display the error (no matter the graceful error or not)
  7.         if($error_type === 0 || $error_type === 2)
  8.         {
  9.             // It over-rides graceful errors
  10.             $this->display_exception($e, $message);
  11.            
  12.             // Killing the script
  13.             exit;
  14.         }
  15.         else
  16.         {
  17.             // It does not over-ride graceful errors
  18.             if(!GRACEFUL_ERRORS || ADMIN_DEBUG)
  19.             {
  20.                 // We are to display the error
  21.                 $this->display_exception($e, $message);
  22.             }
  23.         }
  24.     }
  25.     else
  26.     {
  27.         // Checking if we are to display the error (no matter the graceful error or not)
  28.         if($error_type === 0 || $error_type === 2)
  29.         {
  30.             // It over-rides graceful errors
  31.             $this->display_error($e, $message);
  32.            
  33.             // Killing the script
  34.             exit;
  35.         }
  36.         else
  37.         {
  38.             // It does not over-ride graceful errors
  39.             if(!GRACEFUL_ERRORS || ADMIN_DEBUG)
  40.             {
  41.                 // We are to display the error
  42.                 $this->display_error($e, $message);
  43.             }
  44.         }
  45.     }
  46.    
  47.     // Logging if we need to
  48.     $this->_log($e, $file);
  49.    
  50.    
  51.     // Checking if we are to kill the script or not
  52.     if(DIE_ON_ERR)
  53.     {
  54.         exit;
  55.     }
  56. }

I'm not exactly sure if I got it working perfectly or by the good programming standard, but I do have it all working the way I want. I can modify what gets displayed, to whom it gets displayed, all by a simple definition variables in a separate file (I've tested different settings and they all seem to work fine) so I'm happy with that. And this system seems to work how I want it to.

Any help or pointers in the right direction would be greatly appreciated. Thanks!

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 141 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.