Advertisement
Sk8erPeter

Exceptionnel

Mar 31st, 2012
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. function logErrors($errorArray, $other_params){
  3.     // OK, itt naplózzuk a hibákat
  4.    // .................
  5. }
  6.  
  7. class MySpecialException extends Exception
  8. {
  9.     public function __construct($message, $code = 0, Exception $previous = null){
  10.         $logParams = array('egyiktökmindegy', 'megmégvalami');
  11.         logErrors($message);
  12.         if(is_array($message)){
  13.             $message = implode('<br />', $message);
  14.         }
  15.         parent::__construct($message, $code, $previous);
  16.     }
  17. }
  18.  
  19. class MyOtherSpecialException extends Exception
  20. {
  21.     public function __construct($message, $code = 0, Exception $previous = null){
  22.         $logParams = array('másiktökmindegy', 'megmégvalami');
  23.         logErrors($message);
  24.         if(is_array($message)){
  25.             $message = implode('<br />', $message);
  26.         }
  27.         parent::__construct($message, $code, $previous);
  28.     }
  29. }
  30.  
  31. class MyClass {
  32.     function blabla(){
  33.       // .......
  34.       $errorArray = array();
  35.       if( $hiba_van ){
  36.         $errorArray[] = 'ezért meg azért';
  37.       }
  38.      
  39.       if(!empty($errorArray)){
  40.           throw new MySpecialException($errorArray);
  41.       }
  42.  
  43.       // ...
  44.     }
  45.  
  46.     function masikblabla(){
  47.         // .........
  48.       $errorArray = array();
  49.       if( $hiba_van ){
  50.         $errorArray[] = 'ezért meg azért';
  51.       }
  52.      
  53.       if(!empty($errorArray)){
  54.           throw new MyOtherSpecialException($errorArray);
  55.       }
  56.       // .......................
  57.      
  58.     }
  59. }
  60.  
  61. //**************************************************************************************
  62. // akkor most használjuk is a függvényeinket
  63.  
  64. try{
  65.     $myClass = new MyClass();
  66.     $myClass->blabla();
  67.     $myClass->masikblabla();
  68.    
  69.     // SIKER
  70.     echo 'jé, csak eljutottunk idáig is, minden rendben volt.....';
  71.    
  72. } catch (MySpecialException $e){
  73.     // ....
  74.     echo $e->getMessage();
  75. } catch (MyOtherSpecialException $e){
  76.     // ....
  77.     echo $e->getMessage();
  78. } catch (Exception $e){
  79.     // ....
  80.     echo $e->getMessage();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement