php学习笔记10-Exception Handling

PHP中的异常处理跟JAva可以说没什么两样。不过没尝试过是否可以finally。

# !/usr/bin/php
<? php
#  Exception Handling
#
#
 similar with JAVA
#
#
 Class Exception

echo   " Stage 1: " ;
try  {
        
throw   new   Exception ( " An exception raised! " , 3 );  //  3 is exception code
catch  (  Exception   $ex  ) {
        
echo   " Exception: Code:  " . $ex -> getCode() . " Message:  "
                
. $ex -> getMessage() . " File:  " . $ex -> getFile()
                
. " Line:  " . $ex -> getLine() . " " ;
}
echo   " " ;

class  MyException  extends   Exception  {
        
function  __toString() {
                
return   " Exception: Code:  " . $this -> getCode() . " Message:  "
                        
. $this -> getMessage() . " File:  " . $this -> getFile()
                        
. " Line:  " . $this -> getLine() . " " ;
        }
}

echo   " Stage 2: " ;
try  {
        
throw   new  MyException();
catch  ( MyException  $ex  ) {
        
echo   $ex ;
}
?>

你可能感兴趣的:(java,PHP,exception,function,File,Class)