PHP Exception 클래스

PHP/매뉴얼 번역 2008/05/07 23:20
  • Exception 클래스는 모든 예외들의 베이스클래스 (Exception is the base class for all Exceptions.)
  • Exception 베이스 클래스의 기본 구조는 다음과 같음.

    class Exception {
        /* 속성 */
        //예외 메시지
        protected string $message ;
        //내부 예외명    
        private string $string ;
        //예외 코드    
        protected int $code ;
        //예외가 throw 된 파일명  
        protected string $file ;
        //예외가 throw 된 행번호    
        protected int $line ;
        //스택트레이스  
        private array $trace ;

        /* 메소드 */
        public __construct ([ string $message [, int $code ]] )
        final public string getMessage ( void )
        final public int getCode ( void )
        final public string getFile ( void )
        final public string getLine ( void )
        final public string getTrace ( void )
        final public string getTraceAsString ( void )
       
        /* Overrideable */
        public string __toString ( void )
        final private string __clone ( void )
    }

    • Exception::__construct([ string $message [, int $code ]] )
      예외를 작성
      설명
      string $message
      스로(throw) 하는 예외 메시지

      int $code
      예외 코드

    • Exception::getMessage ( void )
      예외 메시지를 리턴
      예)

      try {
          throw new
      Exception("Some error message");
      } catch(
      Exception $e) {
          echo
      $e->getMessage();
      }

    • Exception::getCode ( void )
      예외코드를 리턴

      try {
          throw new
      Exception("Some error message", 30);
      } catch(
      Exception $e) {
          echo
      "The exception code is: " . $e->getCode();
      }

      The exception code is: 30
    • Exception::getFile ( void )
      예외가 발생한 파일명 리턴

      try {
          throw new
      Exception;
      } catch(
      Exception $e) {
          echo
      $e->getFile();
      }

      /home/bjori/tmp/ex.php

    • Exception::getLine ( void )
      예외가 발생한 행번호 리턴
      try {
          throw new
      Exception("Some error message");
      } catch(
      Exception $e) {
          echo
      "The exception was thrown on line: " . $e->getLine();
      }

      The exception was thrown on line: 3

    • Exception::getTrace ( void )
      예외의 스택트레이스를 리턴
      배열값 리턴

      function test() {
          throw new
      Exception;
      }

      try {
         
      test();
      } catch(
      Exception $e) {
         
      var_dump($e->getTrace());
      }

      array(1) {
        [0]=>
        array(4) {
          ["file"]=>
          string(22) "/home/bjori/tmp/ex.php"
          ["line"]=>
          int(7)
          ["function"]=>
          string(4) "test"
          ["args"]=>
          array(0) {
          }
        }
      }

    • Exception::getTraceAsString ( void )
      예외의 스택트레이스의 문자열 리턴
      문자열값(스트링) 리턴

      function test() {
          throw new
      Exception;
      }

      try {
         
      test();
      } catch(
      Exception $e) {
          echo
      $e->getTraceAsString();
      }

      #0 /home/bjori/tmp/ex.php(7): test()
      #1 {main}

    • Exception::__toString ( void )
      예외의 문자열 표현값을 리턴

      try {
          throw new
      Exception("Some error message");
      } catch(
      Exception $e) {
          echo
      $e;
      }

      exception 'Exception' with message 'Some error message' in /home/bjori/tmp/ex.php:3
      Stack trace:
      #0 {main}



    • Exception::__clone ( void )
      예외의 복사본(클론)을 작성하는 메소드.
      결과적으로는 Fatal 에러발생
      예외는 클론 불가능.
       

영어 페이지 : http://jp.php.net/manual/en/class.exception.php
한글 페이지는 없음.

목차로
PHP 예외 매뉴얼 번역
tags : ,
Trackback 0 : Comment 0