Using try catch - php exceptions

Not many devs use try catch in custom code, especially in drupal 7. Thing is that it can save you from bigger problems if you wrap your (bad) code into that block, so lets just make simple example how to use it.

try {
   // Some code here, that might turn rouge and break other part of function you are calling
}
  catch (Exception $e) {
    // Something went wrong somewhere
    watchdog_exception('message_type', $e);
}

So if you wrap some code in this, even if it goes bad and gets some kind of error, like EntityMetadataWrapperException code around it will still function and you will get proper error. If you dont wrap it around some particular chunk of code and it breaks, the whole function will be broken and you will have more problems and harder time figuring out what is wrong.

Similar thing is with Drupal 8, you just call different class for logging info and type of exception if you are extending it.
 

try {
  // Some code here
}
catch (Exception $e) {
  // Generic exception handling if something else gets thrown.
  \Drupal::logger('widget')->error($e->getMessage());
}

more for drupal 8, reade here https://www.drupal.org/docs/develop/coding-standards/php-exceptions