PHP5 Exception 예외처리 사용 방법과 그 종류

PHP/PHP5 클래스 2012. 4. 3. 22:18

PHP5 Exception 사용 방법과 그 종류


Exception 종류 클래스1

BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
LogicException
OutOfBoundsException
OutOfRangeException
OverflowException
RangeException
RuntimeException
UnderflowException
UnexpectedValueException


Exception 종류 클래스 2
Exception
ErrorException




@ 가장 많이 쓰는 Exception 클래스 소스

Exception {
    /* 프로퍼티 */
   protected string $Exception->message ;
   protected int $code ;
   protected string $file ;
   protected int $line ; 


   /* 메소드 */
   public Exception::__construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
   final public string Exception::getMessage ( void )
   final public Exception Exception::getPrevious ( void )
   final public mixed Exception::getCode ( void )
   final public string Exception::getFile ( void )
   final public int Exception::getLine ( void )
   final public array Exception::getTrace ( void )
   final public string Exception::getTraceAsString ( void )
   public string Exception::__toString ( void )
   final private void Exception::__clone ( void )
}

@ 사용법

/*방법 1*/
try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}


/*방법 2*/
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}


/* 방법 3 */
class Test{
      public init($s){
           if(!empty($s)) 
                 throw new Exception('Division by zero.');
      }
}