Connecting Tech Pros Worldwide Help | Site Map

Exception error handling?

fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
#1: Oct 18 '07
Hey all.. again. :)

I am wondering how to go about getting the errors from the database layer to echo to the UI?

If mysql throws an error, how can I echo that to the browser? Am I correct in going about it like this?

[php]
die($_POST['myerror'] = mysql_error());
echo $_POST['myerror'];
[/php]

Would it be like that or is there some other way of doing this?

Thanks.

Frank
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Oct 18 '07

re: Exception error handling?


You can display a MySQL error right after the MySQL command issued, e.g.
[php]
$conn = mysql_connect("localhost", "xxxx", "yyyy")
or die("Could not connect to the db server: ".mysql_error());

or

$result = mysql_query("myquery") or
die("Query resulted in an error: " . mysql_error());
[/php]
Ronald
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: Oct 18 '07

re: Exception error handling?


Heya, Frank.

In PHP 5, the preferred method for signaling a major malfunction is the Exception class.

Expand|Select|Wrap|Line Numbers
  1. function doQuery( $sql )
  2. {
  3.     global $db;
  4.  
  5.     $__res = mysql_query( $sql, $db );
  6.  
  7.     if( $_err = mysql_error($db) )
  8.     {
  9.         throw new Exception("[doQuery]\nQuery:\n{$sql}\n\nError:\n{$_err}");
  10.     }
  11.  
  12.     // Am I the only person in the universe that does this?
  13.     preg_match_all( '/\\b\\w+\\b/', $sql, $__matches );
  14.     switch( strtolower($__matches[0][0]) )
  15.     {
  16.         case 'select':
  17.             $_resultSet = array();
  18.             while( $__row = mysql_fetch_assoc($__res) )
  19.             {
  20.                 $_resultSet[] = $__row;
  21.             }
  22.         break;
  23.  
  24.         case 'insert':
  25.             $_resultSet = mysql_insert_id($db);
  26.         break;
  27.  
  28.         .
  29.         .
  30.         .
  31.     }
  32.  
  33.     if( is_resource($__res) )
  34.     {
  35.         mysql_free_result($__res);
  36.     }
  37.  
  38.     return $_resultSet;
  39. }
  40.  
fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
#4: Oct 19 '07

re: Exception error handling?


Thanks guys! Now I am started down the right track on exception handling.

What would be the difference if I used php's exception handling try / catch over say, Ronald's (Ronverdonk) suggestion?

What I was wanting to do was to have a general error message screen and update that screen with a session variable. The only way I can see to do that would be to use Ronverdonk's suggestion and save the error into a session then echo that to the browser. Am I wrong?

Is this possible with the catch / try method?


Thanks for the input!

Frank
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Oct 19 '07

re: Exception error handling?


Heya, Frank.

The main advantage to using try/catch is that you can catch Exceptions *anywhere*.

For example (and using the doQuery() function above):
Expand|Select|Wrap|Line Numbers
  1. function runABadQuery()
  2. {
  3.     doQuery( 'this is not valid SQL' );
  4. }
  5.  
  6. ob_start();
  7.  
  8. try
  9. {
  10.     echo 'You will never see this.';
  11.  
  12.     runABadQuery();
  13.  
  14.     echo 'You will never see this, either.';
  15. }
  16. catch( Exception $e )
  17. {
  18.     // Alert admin
  19.     mail
  20.     (
  21.         'my@email.address',
  22.         'Exception thrown: ' . date('F j, Y g:i:s a'),
  23.         $e->getMessage()
  24.     );
  25.  
  26.     // Clear the output buffer.
  27.     while( ob_end_clean() )
  28.     {
  29.     }
  30.  
  31.     // Output an error message.
  32.     echo 'The server made a boo-boo.  This should be your customized 500 error page.  You could even use include() here if you wanted.';
  33.  
  34.     // We're done here.
  35.     exit;
  36. }
  37.  
  38. ob_end_flush();
  39.  
Notice that even though the Exception gets thrown way down the stack in doQuery() [called by runABadQuery() called by main()], the Exception still gets caught.

In the code above, you can enclose the entirety of your code within a try/catch block, and if an Exception gets thrown *anywhere*, your code will stop, it will send an email with the text of the Exception to an administrator, and it will display a generic error page.

Note also that by using output buffering, you can prevent any output from reaching the browser until the script has successfully finished executing.
fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
#6: Oct 19 '07

re: Exception error handling?


Quote:

Originally Posted by pbmods

Heya, Frank.

The main advantage to using try/catch is that you can catch Exceptions *anywhere*.

For example (and using the doQuery() function above):

Expand|Select|Wrap|Line Numbers
  1. function runABadQuery()
  2. {
  3. doQuery( 'this is not valid SQL' );
  4. }
  5.  
  6. ob_start();
  7.  
  8. try
  9. {
  10. echo 'You will never see this.';
  11.  
  12. runABadQuery();
  13.  
  14. echo 'You will never see this, either.';
  15. }
  16. catch( Exception $e )
  17. {
  18. // Alert admin
  19. mail
  20. (
  21. 'my@email.address',
  22. 'Exception thrown: ' . date('F j, Y g:i:s a'),
  23. $e->getMessage()
  24. );
  25.  
  26. // Clear the output buffer.
  27. while( ob_end_clean() )
  28. {
  29. }
  30.  
  31. // Output an error message.
  32. echo 'The server made a boo-boo. This should be your customized 500 error page. You could even use include() here if you wanted.';
  33.  
  34. // We're done here.
  35. exit;
  36. }
  37.  
  38. ob_end_flush();
  39.  
Notice that even though the Exception gets thrown way down the stack in doQuery() [called by runABadQuery() called by main()], the Exception still gets caught.

In the code above, you can enclose the entirety of your code within a try/catch block, and if an Exception gets thrown *anywhere*, your code will stop, it will send an email with the text of the Exception to an administrator, and it will display a generic error page.

Note also that by using output buffering, you can prevent any output from reaching the browser until the script has successfully finished executing.

Pbmods,

thanks for the great explanation. I am going to have to read php's docs on this exception error handling thingy to understand a little better how exactly I can implement this. You give a fantastic explanation but I am unfortunately, still a little lost. :)

I will sit on this for a couple of days and post back with my experiences. :)

Thanks bro!

Frank
Reply