myException
[ class tree: myException ] [ index: myException ] [ all elements ]

example

  1. <?php
  2. /*
  3.  * myException test
  4.  */
  5.  
  6. $iniget = function_exists ('___ini_get') ? '___ini_get' : 'ini_get';
  7. $iniset = function_exists ('___ini_set') ? '___ini_set' : 'ini_set';
  8.  
  9. $cwd = getcwd ();
  10. $ccwd = basename ($cwd);
  11. if ( $ccwd == 'tests' ) {
  12.     $oldpath = $iniget ('include_path');
  13.     $newpath = preg_replace ("!/{$ccwd}!", '', $cwd);
  14.     $iniset ('include_path', $newpath . ':' . $oldpath);
  15. }
  16.  
  17. require_once 'myException.php';
  18.  
  19. # E_ERROR를 myException에서 다루고 싶다면 다음을 처리하도록 한다.
  20. #error_reporting (E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_ERROR);
  21. #register_shutdown_function('myException::myShutdownHandler', 'fatal_error');
  22. #function fatal_error ($dump) {
  23. #    echo '::: Fatal Messages' . PHP_EOL;
  24. #    print_r ($dump);
  25. #}
  26.  
  27. # PHP 자체의 에러를 myException에서 제어하고 싶다면 다음 라인의 주석을 풀어준다.
  28. set_error_handler('myException::myErrorHandler');
  29.  
  30.  
  31. class myEX {
  32.     function foo () {
  33.         try {
  34.             // mysql_connect function이 load 되지 않으면 myException으로
  35.             // 에러 메세지를 전달한다.
  36.             if ( ! function_exists ('mysql_connect') )
  37.                 throw new myException ('Unsupported mysql_connect function', E_USER_ERROR);
  38.  
  39.             /*
  40.              * mysql_connect의 argument가 없기 때문에 argument 관련 E_WARNING
  41.              * 이 발생하며, 기본적으로는 Exception에서 cache가 되지 않는다.
  42.              * PHP Error를 cache하여 myException으로 보내려면, 아래의
  43.              * set_error_handler line의 주석을 풀어줘야 한다.
  44.              */
  45.             $c = mysql_connect ();
  46.         } catch ( Exception $e ) {
  47.             throw new myException ($e->getMessage (), $e->getCode (), $e);
  48.         }
  49.     }
  50. }
  51.  
  52. $m = new myEX;
  53.  
  54. try {
  55.     $m->foo ();
  56. } catch ( Exception $e ) {
  57.     echo $e->Message () . "\n";
  58.     print_r ($e->TraceAsArray ()) . "\n";
  59.     $e->finalize ();
  60. }
  61.  
  62. echo "E_ERROR 또는 E_USER_ERROR 에러가 발생하면 이 라인이 보이지 않습니다.\n";
  63. ?>

Documentation generated on Fri, 30 Aug 2024 06:11:00 +0900 by phpDocumentor 1.4.4