PHP/Statement/Exception

Материал из Web эксперт
Версия от 07:07, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Catching Exceptions in PHP 5

 
<?php
        class MyException extends Exception {
        }
        class AnotherException extends Exception {
        }
        class ThrowExample {
                public function makeMyException() {
                        throw new MyException();
                }
                public function makeAnotherException() {
                        throw new AnotherException();
                }
                public function makeError() {
                        throw new Exception();
                }
        }
        $inst = new ThrowExample();
        try {
                $inst->makeMyException();
        } catch(MyException $e) {
                echo "Caught "MyException"\n";
        }
        try {
                $inst->makeAnotherException();
        } catch(MyException $e) {
                echo "Caught "MyException"\n";
        } catch(AnotherException $e) {
                echo "Caught "AnotherException"\n";
        }
        try {
                $inst->makeError();
        } catch(MyException $e) {
                echo "Caught "MyException"\n";
        } catch(AnotherException $e) {
                echo "Caught "AnotherException"\n";
        }
?>



extends Exception to create your own exception

 
<?php
class MyException extends Exception { 
    function __construct($language,$errorcode) { 
        $this->language = $language;
        $this->errorcode = $errorcode;
    } 
    function getMessageMap() {
        $errors = file("errors/".$this->language.".txt");
        foreach($errors as $error) {
             list($key,$value) = explode(",",$error,2);
             $errorArray[$key] = $value;
        }
        return $errorArray[$this->errorcode];
    }  
}
try {
    throw new MyException("english",4);
}
catch (MyException $e) {
    echo $e->getMessageMap();
}
?>



Using Custom Exceptions to Handle Different Circumstances

 
<?php
class MyException extends Exception {
  public function summarize() {
    $ret = "<pre>\n";
    $ret .=  "msg: ".$this->getMessage()."\n"
         ."code: ".$this->getCode()."\n"
         ."line: ".$this->getLine()."\n"
         ."file: ".$this->getFile()."\n";
    $ret .= "</pre>\n";
    return $ret;
  }
}
class FileNotFoundException extends MyException { }
class FileOpenException extends MyException { }
class Reader {
  function getContents( $file ) {
      throw new FileNotFoundException( "could not find "$file"" );
      throw new FileOpenException( "unable to open "$file"" );
  }
}
$reader = new Reader();
try {
  print $reader->getContents( "blah.txt" );
} catch ( FileNotFoundException $e ) {
  print $e->summarize();
} catch ( FileOpenException $e ) {
  print $e->summarize();
} catch ( Exception $e ) {
  die("unknown error");
}
?>



Using PHP 5 exception handling

 
<?php 
function errors_to_exceptions($code, $message) { 
   throw new Exception($code, $message); 
} 
set_error_handler("errors_to_exceptions"); 
try { 
  $connection = mysql_connect($host, $user, $password); 
  mysql_select_db($database, $connection); 
  $query = "SELECT page_id,link_text,parent_id FROM menus WHERE page_id="$pid""; 
  $result = mysql_query($query); 
  if(mysql_num_rows($result) == 0) 
     echo "Invalid page request -- click <a href=\"" . $_SERVER["PHP_SELF"] . "?pid=1\">here</a> to continue.</h2>\n"; 
  else { 
     $value = mysql_fetch_object($result); 
  } 
}catch (Exception $e) { 
    printf("<p>Caught exception: %s.</p>\n", $e->getMessage()); 
} 
?>