Try catch question

I have this inside of a class, and i know the $host, $name, $pass, $db should be $this->, but the try catch doesn’t seem to work. Am I using it wrong?

	function connect()
	{
		try {	
			$link = mysql_connect($host, $name, $pass);
			$db = mysql_select_db($db, $link);
		} catch (Exception $e) {
			echo ('An error has occured ('.$e.'): '.mysql_errno());		
		}		
	}

If I turn on error reporting, i get connection errors, but if i turn off error reporting, i get nothing. Why doesn’t this work?

The MySQL extension does not throw exceptions…

You could work around this by using


try
{
$db = mysql_connect( ... ) or throw new Exception('Error Connecting');
mysql_select_db($db, $link) or throw new Expection('Cant Select DB');
}
catch( Exception $e )
{
echo '...';
}

or you can use the PDO MYSQL Extension
http://nz2.php.net/manual/en/ref.pdo.php

or throw new Exception(‘Error Connecting’);

Ah I see, thank you very much!

Hmm I am getting an error…

Parse error: syntax error, unexpected T_THROW

$link = mysql_connect($this->host, $this->name, $this->pass) or throw new Exception(‘Cannot connect to MySQL’);
mysql_select_db($this->db, $link)

try this:
$link = mysql_connect($this->host, $this->name, $this->pass);
if(!$link) {
throw new Exception(‘Cannot connect to MySQL’);
}


$con = mysql_connect();

# How important is the connection to the database? Show the application take
# a nose dive if it cannot connect? One needs to weight the options of error handling.

if ($con === false) {

    $error = 'Failed to connect to the MySQL database. (MySQL Error Code: %s)';
    $error = sprintf($error, mysql_errno());

    # The connection is very important:
    trigger_error($error, E_USER_ERROR);

    # Important but not that important:
    trigger_error($error, E_USER_WARNING);

    # And not important at all:
    trigger_error($error, E_USER_NOTICE);

    # I recommend that a DB error page be display if this goes bad.

}

Throwing exceptions in a try catch block just looks messy and fugly to me.
Maybe you should think about using PDO or Mysqli

Just install a customer error-handler to convert all errors into exceptions:


error_reporting(E_ALL | E_STRICT);
function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exceptions_error_handler');

This way, you only need to worry about one type of error-handling.

Because legacy php “errors” (warnings, notices) are not automatically converted to exceptions. You need an “errors-to-exceptions” error handler to get the exceptions thrown.


set_error_handler(create_function('$severity, $message, $filename, $lineno', '
    throw new ErrorException($message, 0, $severity, $filename, $lineno); 
'));

ed: I’ve seen forums that implement a feature that warns you when the thread you’re posting on was updated while you composed your message. Very useful actually!

hehe … indeed