zoukankan      html  css  js  c++  java
  • 异常处理 简单

    <?php
    //header("Content-type: text/html; charset=utf-8");
    header("Content-type:text/html; charset=utf-8");
    //////////////////////////////////////
    //@date: 2013-02-21
    //异常处理
    //PHP5添加了类假似于其它语言的异常处理模块,在PHP代码中所产生的异常可被throw语句抛出并
    //catch语句捕获,需要进行异常处理的代码都必须放入到try代码块中
    //以便宜捕获可能存在的异常,每一个try到少要有一个与之对应的catch,使用多个catch可以捕获不同的类所产生的异常,当try代码块不再抛出异常或者找不到catch能匹配所抛出的异常时,PHP代码就会跳转到最后一个catch的后面继承执行,当然,PHP允许在catch代码块中再次抛出(throw)异常
    
    // function inverse($x)
    // {
    // 	   if(!$x){
    // 	   	  throw new Exception("Division by zero.");
    // 	   }else
    // 	       return 1 / $x;
    // }
    
    // try{
    //     echo inverse(5)."<BR><BR>";
    //     echo inverse(0);    
    // }catch(Exception $e){
    // 	echo "info: ", $e->getMessage(),"<BR><BR>";
    // }
    // echo "Hello world<BR><BR>";
    
    
    // class myException extends Exception{}
    
    // class Test
    // {
    //     public function testing()
    //     {
    //     	   try{
    //                try{
    //                    throw new myException("foo!");
    //                    //这个throw抛给下面的catch
    //                }catch(myException $e){
    //                	   throw $e; //这个throw抛给外面一层的catch
    //                }
    //     	   }catch(Exception $e){
    //     	   	    var_dump($e->getMessage());
    //     	   }
    //     }
    // }
    // $foo = new Test;
    // $foo->testing();
    
    
    //定义一个接口类
    // interface IException
    // {
    //      public function getMessage();
    //      public function getCode();
    //      public function getFile();
    //      public function getLine();
    //      public function getTrace();
    //      public function getTraceAsString();
    
    //      public function __toString();
    //      public function __construct($message=null, $code=0);
    // }
    
    // //定义一个抽象类
    // //abstruct
    // //implements  从接口中继承
    // abstract class CustomException extends Exception implements IException
    // {
    //      protected $message = "Unknown exception";
    //      private $string;
    //      protected $code = 0;
    //      protected $file;
    //      protected $line;
    //      protected $trace;
    
    //      public function __construct($message = null, $code = 0)
    //      {
    //      	    if(!$message){
    //      	    	  throw new $this("Unknown".get_class($this)); //返回自己的class名称
    //      	    }
    //      	    parent::__construct($message, $code);
    //      }
    
    //      public function __toString()
    //      {
    //      	    return get_class($this)."'{$this->message}' in {$this->file}({$this->line})\n".$this->getTraceAsString();
    //      }
    // }
    
    // //继承
    // class TextException extends CustomException{};
    
    // function exceptionText()
    // {
    // 	   try{
    //            throw new TextException();
    
    // 	   }catch(TextException $e){
    // 	   	   echo "TestException ({$e->getMessage()})\n{$e}<BR><BR>";
    // 	   }
    // 	   catch(Exception $e){
    // 	   	    echo "Exception ({$e->getMessage()})\n{$e}<BR><BR>";
    // 	   }
    // }
    // exceptionText();
    
    
    // class MyException extends Exception
    // {
    // }
    
    // class Tester
    // {
    //      public function foobar()
    //      {
    //           try{
    //                $this->helloWorld();
    //           }catch(MyException $e){
    //                //throw new Exception("Problem in foober", 0, $e);
    //                throw new Exception("Problem in foober", 0);
    //           }
    //      }
    
    //      protected function helloWorld()
    //      {
    //           throw new MyException('Problem in helloWorld()');
    //      }
    // }
    
    // $tester = new Tester();
    // try
    // {
    //     $tester->foobar();
    // }catch(Exception $e)
    // {
    //      echo $e->getTraceAsString();
    //      echo $e->getMessage();
    // }
    
    
    // $name = "Name";
    // try
    // {
    //     try{
    //          if(preg_match('/[^a-z]/i', $name)){
    //               throw new Exception("$name 开始字母是从a-z A-Z");
    //          }
    //          if(strpos(strtolower($name), 'name') !== FALSE){
    //               throw new Exception("$name 包含name关键字");
    //          }
    //          echo "这个用户有效!";
    
    //     }catch(Exception $e){
    //          //throw new Exception("插入name再次!", 0, $e);
    //          throw new Exception("插入name再次!", 0); 
    //     }
    // }catch(Exception $e)
    // {
    //     // var_dump(get_class_methods($e));
    //     // array(8) { [0]=> string(11) "__construct" [1]=> string(10) "getMessage" [2]=> string(7) "getCode" [3]=> string(7) "getFile" [4]=> string(7) "getLine" [5]=> string(8) "getTrace" [6]=> string(16) "getTraceAsString" [7]=> string(10) "__toString" } 
    //     //我干,这个也没见过一个方法名叫getPrevious()的方法名啊! 
    
    
    //     // if ($e->getPrevious()) //判断是否有前一个异常
    //     // {
    //     //     echo "前一个异常是:".$e->getPrevious()->getMessage()."<BR>";
    //     // }
    //     echo "异常是:".$e->getMessage()."<BR>";
    // }
    
    
    
    // class DisplayException extends Exception{};
    // class FileException extends Exception{};
    // class AssessCotrol extends FileException{};
    // class IOError extends FileException{};
    
    // try
    // {
    //      if(!is_readable($somefile)){ //判断给定的文件名是否可读
    //         throw new IOError("文件不可读");
    //      }  
    //      if(!user_has_access_to_file($someuser, $somefile)){
    //           throw new AssessCotrol("权限被拒绝!");
    //      }
    //      if(!display_file($somefile)){ 
    //           throw new DisplayException("无法显示文件");
    //      }
    
    // }catch(FileException $e){
    //      echo "File error:".$e->getMessage();
    //      exit(1);
    // }
    
    
    ///////////////////////////////////////////////////
    //@date: 2013-02-23
    //扩展PHP内置的异常处理类
    //自定义一个异常处理类
    // class MyException extends Exception
    // {
    //      public function __construct($message, $code = 0)
    //      {
    //           parent::__construct($message, $code);
    //      }
    
    //      public function __toString()
    //      {
    //            return __CLASS__.":[{$this->code}]:{$this->message}<BR><BR>";
    //      }
    //      public function customFunction()
    //      {
    //           echo "这种类型的异常是自定义函数!";
    //      }
    // }
    
    // class TestException
    // {
    //      public $var; 
    //      const THROW_NONE = 0;
    //      const THROW_CUSTOM = 1;
    //      const THROW_DEFAULT = 2;
    
    //      function __construct($avalue = self::THROW_NONE)
    //      {
    //           switch($avalue){
    //                case self::THROW_CUSTOM:
    //                     throw new MyException("叁数为1 ", 5);
    //                     break;
    
    
    //                case self::THROW_DEFAULT:
    //                     throw new Exception("参数为2 ", 6);
    //                     break;
    
    //                default:
    //                     //没有异常的情况下,创建一个对象
    //                     $this->var =  $avalue;
    //                     break;
    //           }
    //      }
    // }
    // //例子1
    // try{
    //      $o = new TestException(TestException::THROW_CUSTOM);
    // }catch(MyException $e){
    //      echo "引用异常<BR><BR>";
    //      echo $e->getMessage();
    //      $e->customFunction();
    // }catch(Exception $e){
    //      echo "被忽略";
    // }
    // var_dump($o);
    // echo "<BR>***********************<BR><BR><BR><BR>";
    
    
    // try{
    //      $o = new TestException(TestException::THROW_DEFAULT);
    // }catch(MyException $e){
    //      echo "不能匹配异常的种类,被忽略!<BR><BR>";
    //      echo $e->getMessage();
    //      $e->customFunction();
    
    // }catch(Exception $e){
    //      echo "匹配异常!<BR><BR>";
    //      echo $e->getMessage();
    // }
    // var_dump($o);
    // echo "<BR>***********************<BR><BR><BR><BR>";
    
    
    
    // try{
    //      $o = new TestException(TestException::THROW_CUSTOM);
    // }catch(Exception $e){ //在没有选择的情况下,基类Exception也是可以捕获的
    //      echo "捕获异常!";
    //      echo $e->getMessage();
    // }
    // var_dump($o);
    // echo "<BR>***********************<BR><BR><BR><BR>";
    
    // try{
    //      $o = new TestException(TestException::THROW_DEFAULT);
    // }catch(MyException $e){
    //      echo "应该不能捕获,因为类型不匹配!<BR>";
    //      //直接提示错误
    //      echo $e->getMessage();
    // }
    
    
    // try{
    //      $o = new TestException();     
    // }catch(Exception $e){
    //      echo "没有异常!";
    // }
    // var_dump($o);
    
    
    // error_reporting(E_ALL-E_NOTTCE);
    // class myCustomException extends Exception
    // {
    //      public function __construct($message, $code =0)
    //      {
    //           parent::__construct($message, $code);
    //      }
    
    //      public function __toString()
    //      {
    //           return "<b style='color:red'>".$this->message."</b>";
    //      }
    // }
    
    // class testException
    // {
    //      public function __construct($x)
    //      {
    //           $this->x = $x;
    //      }
    //      function see()
    //      {
    //           if($this->x ==9){
    //                throw new myCustomException("i didnt like it");
    //           }
    //      }
    // }
    
    // $obj = new testException(9);
    // try{
    //      $obj->see();
    // }catch(myCustomException $e){
    //      echo $e;
    // }
    
    
    class TestableException extends Exception
    {
         private $property;
    
         function __construct($property)
         {
              $this->protected = $property;
              parent::__construct($this->format($property));
         }
         function format($property)
         {
              return "I have formatted: ".$property."!!";
         }
         function getProperty()
         {
              return $this->property;
         }
    }
    function testSomethingThrowsTestableException()
    {
         try
         {
             throw new TestableException('Property');
         }catch(TestableException $e){
              echo $e->getMessage();
         }
    }
    testSomethingThrowsTestableException();
    
    ?>
    

      

  • 相关阅读:
    NSString常用方法
    iOS8 【xcode6中添加pch全局引用文件】
    Size Classes with Xcode 6
    推荐学习网站
    Objective C类方法load和initialize的区别
    iOS开发中,单元测试的好处!
    iOS 9.0中UIAlertController的用法。
    iOS 9.0 数组中所有对象共同执行方法的接口不可用
    Xcode 7.0以后 iOS 9.0以后 最新百度地图 详细使用
    iOS9适配关于URL Schemes
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2923784.html
Copyright © 2011-2022 走看看