标签:
读《PHP和MySQL Web开发》笔记合集:
http://my.oschina.net/bluefly/blog/478580
1、异常处理概念
|  
      
        <?php 
        
      
        try  { 
        
      
          throw new Exception("A terrible error has occurred", 42); 
        
      
        } 
        
      
        catch (Exception $e) { 
        
      
          echo "Exception ". $e->getCode(). ": ". $e->getMessage()."<br />". 
        
      
          " in ". $e->getFile(). " on line ". $e->getLine(). "<br />"; 
        
      
        } 
        
      
        ?> 
        
       |  
   
 
  
2、Exception类
 
     Exception(PHP 5 >= 5.1.0) 简介Exception是所有异常的基类。 类摘要 
          Exception { 
           
         
           /* 属性 */ 
           
         
           protectedstring
          $message ; 
           
         
           protectedint
          $code ; 
           
         
           protectedstring
          $file ; 
           
         
           protectedint
          $line ; 
           
         
           /* 方法 */ 
           
         
           public
          __construct ([ string $message = "" [, int $code = 0 [, 
          Exception$previous = 
          NULL ]]] ) 
           
         
           final public string 
          getMessage ( void ) 
           
         
           final public Exception 
          getPrevious ( void ) 
           
         
           final public int 
          getCode ( void ) 
           
         
           final public string 
          getFile ( void ) 
           
         
           final public int 
          getLine ( void ) 
           
         
           final public array 
          getTrace ( void ) 
           
         
           final public string 
          getTraceAsString ( void ) 
           
         
           public string
          __toString ( void ) 
           
         
           final private void 
          __clone ( void ) 
           } 
        属性message异常消息内容 code异常代码 file抛出异常的文件名 line抛出异常在该文件中的行号 Table of Contents
  |  
   
|  
      
        <?php 
        
      
        class myException extends Exception 
        
      
        { 
        
      
          function __toString() 
        
      
          { 
        
      
               return "<table border=\"1\"> 
        
      
               <tr> 
        
      
               <td><strong>Exception ".$this->getCode()." 
        
      
               </strong>: ".$this->getMessage()."<br />"." 
        
      
               in ".$this->getFile()." on line ".$this->getLine()." 
        
      
               </td> 
        
      
               </tr> 
        
      
               </table><br />"; 
        
      
           } 
        
      
        } 
        
      
        try 
        
      
        { 
        
      
          throw new myException("A terrible error has occurred", 42); 
        
      
        } 
        
      
        catch (myException $m) 
        
      
        { 
        
      
           echo $m; 
        
      
        } 
        
      
        ?> 
        
       |  
   
|  
      
        // open file for appending 
        
      
        try 
        
      
        { 
        
      
          if (!($fp = 
       @fopen("$DOCUMENT_ROOT/../orders/orders.txt", ‘ab‘))) 
        
      
              
       throw new fileOpenException(); 
        
      
          if (!flock($fp, LOCK_EX)) 
        
      
             
       throw new fileLockException(); 
        
      
          if (!fwrite($fp, $outputstring, strlen($outputstring))) 
        
      
             
       throw new fileWriteException(); 
        
      
          flock($fp, LOCK_UN); 
        
      
          fclose($fp); 
        
      
          echo "<p>Order written.</p>"; 
        
      
        } 
        
       
       catch (fileOpenException $foe) 
        
      
        { 
        
      
           echo "<p><strong>Orders file could not be opened. 
        
      
                 Please contact our webmaster for help.</strong></p>"; 
        
      
        } 
        
       
       catch (Exception $e) 
        
      
        { 
        
      
           echo "<p><strong>Your order could not be processed at this time. 
        
      
                 Please try again later.</strong></p>"; 
        
      
        } 
        
      |  
   
|  
      
         if (!($fp = 
       @fopen("$DOCUMENT_ROOT/../orders/orders.txt", ‘ab‘))) 
        
      
              
       throw new fileOpenException(); 
        
       |  
   
标签:
原文地址:http://my.oschina.net/bluefly/blog/522358