Posts

Showing posts with the label exceptions

Throw your own - Nested exceptions

If you think that working with exceptions is hard to understand, exception nesting will look even harder (nested exceptions, chained exceptions, exception bubbling - it is all the same). Well, it is a step above, and it can be used if your application is structured in several layers, but at the end you'll see that it is a concept which is here to make your life easier. The basic syntax try { // do something that can go wrong } catch (Exception $e) { throw new Exception( 'Something really gone wrong', 0, $e); } Yes, you could see that kind of example at php.net, but it still does not make much sense. Why you caught it at first place, just to throw a new one? To be honest, it is a bad example (just like the examples on php.net). No wonder that you can not understand it. But lets stick on it for one more moment. First, you caught exception, like in any other try catch block, and than you passed it to an another one. It means that the second exception has reference to the ...

Throw your own - Custom Exceptions

The key to understanding custom exception concept lies in knowing how to catch them. You have to know syntax, and you have to know how to use it. First, the syntax itself try {} catch (CustomException $e) {} catch (OtherException $e) {} catch (Exception $e) {} Basicaly it looks like an switch/case statement. The above code could be written as the next one, and all will be valid and will work perfectly. The try catch syntax is actually automatically handled multiple if statement! try {} catch (Exception $e) { if ($e instanceof CustomException) {} else if ($e instanceof OtherException) {} else {} } Throw your own Note that by distinguishing exceptions you can decide which are the fatal errors and which exceptions are only passing you an information that something is wrong and it is a handled case. This means that you have to use Exceptions not only on critical situations. Throw them for your handled situations too. class MyAuthException extends Exception {} class MyTest { pri...

Throw your own - Losing the fear of Exceptions

The best way to lose the fear of exceptions is to start throwing them. Recipe is simple: whenever you come to case that something is wrong and your function or method can not accomplish its task, just throw an exception. Example 1 My favorite case is when I'm using a configuration values in my applications. Ops, there is no value at all, not an numeric value? I'll just throw an exception and stop thinking about that. This case is especially important if your application will have several installations. This way you are ensuring that nobody (including you) can not forget to fill in configuration. Or, when such case happen anyway, problem will be immediately identified. class MyIni { private $_config; public function __construct( $config) { $this->_config = $config; } public function getProductId() { if (!isset($this->_config['productId'])) throw new Exception( 'Required config parameter [productId] is not set'); if (!is_numeric( $this...

Basic exception usage in php

Theory If you have not already, check the official php documentation on exceptions at http://php.net/manual/en/language.exceptions.php. Besides some php specifics, here you can find basic example how to use try catch blocks and how to work with exceptions. Example goes like this: function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo 'Hello World'; Through this example you can see that if a process finds itself in a problematic situation, it can throw an exception. This exception can be caught, and that way we will prevent system to run into some php fatal error. This are the theoretical basics, however, the real question is how and where to use exception handling in your  applications and scripts. Front Controller Front C...

Why exception handling is important in php?

    I work as a professional programmer for 10 years and in those 10 years I met a bunch of different developers. In plenty of them, especially in web / php developers, I could notice a lack of understanding of the concept of throwing and catching exceptions and what does it serves for. Of course, the first I noticed it at myself. In this blog I would like to point out a number of useful things that proper use of the exception can make, and which were slipping from my hands for a few years too.      Exceptions are providing a standardized way to control problematic processes and they are reducing the possibility of fatal errors in the application . If process throws exception, you are forced to handle situation in your application, and it will automatically reduce the possibility that some problem sleeps out whit out being processed. When you're working with exceptions, you are handling problems at low level, so it will not make you problems with your highe...