Jump to content

Archived

This topic is now archived and is closed to further replies.

ballhogjoni

Are try catch blocks important anymore?

Recommended Posts

Just wondering what everyones opinion is. If you are creating unit tests and running code coverage do you need a try catch block? It seems like unnecessary code to me and creates an unnecessary processing cost.

 

Before you say something like "what about the times your code fails for unknown reasons?", from my experience the times your code fails for unknown reasons it's usually not a bug in your code but a connection problem to a db or missing data or something. For those types of problems you should have other checks in place, right?

 

Am I way out on a limb here or do you guys concur?

 

Just trying to improve my programming knowledge.

 

Thanks

Share this post


Link to post
Share on other sites

Exceptions are one way of dealing with problems. If you don't use exceptions then you wouldn't use try/catch.

- Exceptions give you a lot of flexibility for dealing with error conditions, but if you don't catch one then your script will crash.

- Tracking error codes somewhere (eg, public $errno) are also flexible but if you don't check for them then your application will crash because it doesn't handle error conditions.

- Return values are the most common with PHP and force you to handle error conditions, but either they're limited in power or they make your code look crazy because of all the types of things a function can return.

Share this post


Link to post
Share on other sites

Just wondering what everyones opinion is. If you are creating unit tests and running code coverage do you need a try catch block? It seems like unnecessary code to me and creates an unnecessary processing cost.

 

If you're using exceptions you need to have at least one try/catch at the top level of your scripts to catch general errors (or use set_exception_handler()). That way you can log the error and present a nice page for the end user, or at least fail without dumping a stack trace to the end users screen.

 

Before you say something like "what about the times your code fails for unknown reasons?", from my experience the times your code fails for unknown reasons it's usually not a bug in your code but a connection problem to a db or missing data or something. For those types of problems you should have other checks in place, right?

It's possible for things to fail for other unexpected reasons. For instance if a particular table in mysql gets corrupt, you will not encounter an error until you actually try and query that table. The connection will go through fine, other queries not using that table will go through fine.

 

If you have a script that takes a few seconds to run, what about the case when your connection to the DB goes fine, but then the network link goes down (maybe a router explodes somewhere) before you are done using the database? Your queries will start failing due to the connection having failed.

 

You always need to ensure you handle the possible errors, regardless of how [un]likely they are to happen. Whether you need to handle them right at the spot of occurrence or further up the stack depends on what the error is, whether you can do anything about it, and what kind of error reporting is in place.

 

The nice thing about exceptions when used correctly is that you can choose to handle an error using a local try/catch or just let it bubble up to the top-level handler and be handled there. For example, if a query fails due to a connection problem you can attempt to reconnect and re-try:

//Assume there is a specific ConnectionFailedException() for connection problems, and some other exceptions for other problems
for ($retryCount=0,$maxRetries=3,$success=false; !$success && $retryCount<$maxRetries; $retryCount++){
   try {
      //$db is a connection established successfully previously in the script
      $result = $db->query('SELECT foo FROM bar'); //Link has gone down so this throws a ConnectionFailedException()
      $data = $result->fetch();
      //do stuff
      $success=true;
   }
   catch (ConnectionFailedException $ex){   //We catch only ConnectionFailedException's becasuse it's the only one we can potentially resolve.
                                            //Other problems such as TableCorruptException go to the top level handler.
      //pause a moment so a network problem has a chance to resolve itself.
      sleep(2);
      //Try to connect again.
      $db->reconnect();
   }
}
if (!$success){
   throw $ex; //forward the last exception up to the global handler.
}

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.