i have seen implementation exceptions like:
<?php class teste extends exception {} class mytest { public function __construct() { try{ throw new teste('this exception!'); }catch(exception $exc){ var_dump($exc); } echo '2'; } } $o = new mytest();
where custom exception class teste
defined kept empty above. purpose achieve? have used:
throw new exception('this exception')
instead of
throw new teste('this exception')
extending exception
class useful because allows finer control on exception handling.
the class posted can used this:
try { // code can throw exception, invalidargumentexception or teste // depending on specific issue encountered } catch (teste $e) { // teste thrown when specific issue happens; requires particular handling // f.e. can logged , ignored because program can continue } catch (invalidargumentexception $e) { // exception signals coding error; // example, invalid type of argument passed function // needs different handling teste; f.e. can logged // , notification email sent developer } catch (exception $e) { // generic exception type // catches exceptions not caught other branches // specific handling not possible them; log them , exit program }
on other hand, class exception
contains members , methods need use them. there no need add more members , method.
this why exception
class extended children classes not provide new functionality (most of times).
Comments
Post a Comment