Page 1 of 1

When to use Exceptions

Posted: Wed Sep 08, 2010 4:37 pm
by MichaelR
I've recently decided to consider throwing Exceptions for errors but fail to see the benefit. Below are two sets of code (the class not included because it's not really relevant), and I'd appreciate any feedback regarding whether or not I am using Exceptions correct and, if so, how it trumps traditional error handling:

Code: Select all

<?php 

  try {
    $connection = MySQLConnection::setConnection('******', '******', '******');
  }

  catch (Exception $connectionError) {
    echo $connectionError->getMessage();
  }

  if (isset($connection) && $connection instanceof MySQLConnection) {

    try {
      $connection->setDatabase('database');
    }

    catch (Exception $databaseError) {
      echo $databaseError->getMessage();
    }

  }

?>

Code: Select all

<?php

  $connection = MySQLConnection::setConnection('******', '******', '******'));

  if (!$connection->isConnected()) {
    echo 'Unable to connect to the server';
  }

  elseif (!$connection->setDatabase('database')) {
    echo 'Unable to select the database';
  }

?>
Thanks.

Re: When to use Exceptions

Posted: Wed Sep 08, 2010 6:03 pm
by Christopher
The main difference is that an exception communicates the error through multiple call levels. With return values you are responsible for bubbling the error back up the call stack. So in your example, using an exception may simplify the code within the MySQLConnection if the error occurs within a composited object, or in this code if the error needs to be handled somewhere other than immediately after the call.

Re: When to use Exceptions

Posted: Thu Sep 09, 2010 4:58 pm
by MichaelR
I see what you mean. Thanks. And I suppose my example above would have been better written as:

Code: Select all

  try {

    $connection = MySQLConnection::setConnection('******', '******', '******');

    $connection->setDatabase('database');

  }

  catch (Exception $mysqlError) {
    echo $mysqlError->getMessage();
  }
?

Re: When to use Exceptions

Posted: Fri Sep 10, 2010 12:55 am
by Hanse
I think what many people new to exceptions do wrong, is to always use try-catch blocks. That is unnecessary when you're only going to echo getMessage(). You could create a custom exception handler via set_exception_handler() and handle them in such cases. If you are going to do something else when an exception is thrown, it absolutely OK to use a try-catch. In these examples, I think it is best to just throw them without catching them and let an exception handler do something with them instead.

Re: When to use Exceptions

Posted: Fri Sep 10, 2010 3:40 am
by requinix
Hanse wrote:I think what many people new to exceptions do wrong, is to always use try-catch blocks. That is unnecessary when you're only going to echo getMessage(). You could create a custom exception handler via set_exception_handler() and handle them in such cases. If you are going to do something else when an exception is thrown, it absolutely OK to use a try-catch. In these examples, I think it is best to just throw them without catching them and let an exception handler do something with them instead.
An exception handler is a last resort. You can't recover from a problem by catching an exception that late.
Since exceptions are akin to errors, isn't what you're suggesting to not do error checking?

Re: When to use Exceptions

Posted: Fri Sep 10, 2010 5:31 am
by Hanse
I don't see no reason to catch the exception just to tell the user something went wrong. If you want to do something special when the error occurs (f.ex. rolling back a transaction) it is beneficial, but if it is just to show the message, I'd rather create a super awesome exception handler used in development mode with all info for debugging and a simple one in production. Maybe this is bad, though.