Posts

Showing posts from September, 2011

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