PHP ?? ??
??? ??? ??? ??? ? ????? ???? ??? ???? ? ?????.
??? ??????
PHP 5? ??? ?? ?? ?? ?? ??? ?????.
?? ??? ??? ??(??) ??? ???? ? ????? ???? ??? ???? ? ?????. ??? ??? ???? ???.
??? ????? ????? ??? ?? ?? ?????.
· ?? ?? ??? ?????.
· ?? ??? ?? ??? ??? ?????. (?? ??) ?? ??? ??
· ??? ?? ???? ??? ?? ???? ?? ??? ?? ?????, ???? ??? ?????, ?? ? ?? ???? ???? ??? ??? ? ????
??? ?? ?? ??? ????????:
· ??? ?? ??
· ??? ?? ?? ??? ???
· ?? ??
· ?? ?? ??
· ??? ?? ??? ??
?? : ??? ?? ????? ???? ?? ?? ??? ???? ? ????? ? ???. ?? ??? ??.
??? ?? ???
??? ???? ?? ??? ?? ???? ??? PHP? ???? "catch" ?? ??? ???? ?????.
Exception? catch?? ?? ?? ??? set_Exception_handler()? ??? ??? ??? ??? ??(??? ??)? ???? "Uncaught Exception"? ?????(uncaught
??) ?? ??????.
catch?? ?? ??? ?? ?????.
<?php // 創(chuàng)建一個(gè)有異常處理的函數(shù) function checkNum($number) { if($number>1) { throw new Exception("Value must be 1 or below"); } return true; } // 觸發(fā)異常 checkNum(2); ?>
? ??? ??? ??? ??? ?????.
???? ??: '?? ??' ???? ?? ???? ?? ?? '??' D:WWW11.php:7?? '1 ??'? ??? ???. ?? ??: #0 D:WWW11.php(13): checkNum(2) #1 {??} 7?? ?? D:WWW11.php? ?????
????, ???, ????
?? ??? ? ? ?? ??? ????? ??? ??? ??? ???? ???. ??.
??? ?? ?? ??? ??? ???? ???.
1. Try - ??? ???? ??? "try" ?? ?? ?? ??? ???. ??? ????? ??? ??? ???? ?? ?????. ??? ??? ????? ??? ?????.
2. Throw - ??? ????? ??? ?????. ? "???"? ??? ??? "??"? ???? ???.
3. Catch - "catch" ?? ??? ??? ???? ?? ??? ??? ??? ?????.
????
??? ?????:
<?php header("Content-type:text/html;charset=utf-8"); // 創(chuàng)建一個(gè)有異常處理的函數(shù) function checkNum($number) { if($number>1) { throw new Exception("變量值必須小于等于 1"); } return true; } // 在 try 塊 觸發(fā)異常 try { checkNum(2); // 如果拋出異常,以下文本不會(huì)輸出 echo '如果輸出該內(nèi)容,說(shuō)明 $number 變量'; } // 捕獲異常 catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } ?>
? ??? ??? ?? ??? ??????:
???: ?? ?? 1?? ??? ??? ???
Comment: ??? ?? ?? ????? ?? ??
?? ??:
? ??? ??? ????? ?? ?????.
1 .checkNum() ??? ????. ??? 1?? ?? ??? ?????. ???? ??? ??????.
2. "try" ?? ???? checkNum() ??? ?????.
3. checkNum() ???? ??? ?????.
4. catch" ?? ??? ??? ???? ?? ??? ??? ??($e)? ?????.
5. ? ?? ???? $e->getMessage? ?????( ), ???? ?? ???? ?????.
??? "? ??? catch? ???? ???"?? ??? ??? ?? ??? ??? ????? ??? ?? ???? ??? ? ????. >
??? ?? ?? ??? ??
??? ?? ?? ???? ??? ?? ?? ????. ??? PHP?? ??? ??? ? ??? ???? ?? ???? ??????. ? ???? ?? ???? ????? ???.
? ??? ?? ?? ???? PHP ?? ???? ?? ??? ???? ??? ??? ?? ??? ??? ? ????.
?? ???? ??? ??? ?????.
<?php header("Content-type:text/html;charset=utf-8"); class customException extends Exception { public function errorMessage() { // 錯(cuò)誤信息 $errorMsg = '錯(cuò)誤行號(hào) '.$this->getLine().' in '.$this->getFile() .': <b>'.$this->getMessage().'</b> 不是一個(gè)合法的 E-Mail 地址'; return $errorMsg; } } $email = "someone@example...com"; try { // 檢測(cè)郵箱 if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) { // 如果是個(gè)不合法的郵箱地址,拋出異常 throw new customException($email); } } catch (customException $e) { //display custom message echo $e->errorMessage(); } ?>
? ? ???? ?? ?? ???? ??????. , errorMessage() ??. ?? ???? ????? ??? ?? ???? ??? ???? ?????, getLine(), getFile(), getMessage() ? ?? ???? ???? ??? ? ????.
getLine(): ??? ??? ???? ??? ? ??? ?????.
getFile(): ??? ??? ?????. ??? ??? ???? ??
getMessage(): ?? ??? ?? ????
?? ??:
? ??? ??? ????? ??? ?? ?? ???? ?? ?? ?????.
1 customException() ???? ??? ????. ???? ???????. ???? ???? ???????. ?? ???? ?? ?? ???? ?? ??? ???? ?????.
2. errorMessage() ??? ?????. ??? ??? ???? ??? ? ??? ?? ???? ?????.
3. $email ??? ??? ??? ?? ???? ?????.
4. ??? ??? ????? "try" ?? ??? ???? ??? ??????.
5. "catch" ?? ??? ??? ???? ?? ???? ?????.
?? ??
????? ?? ??? ???? ?? ??? ??? ? ????.
?? if..else ?? ???? ??? ?? ??? ????? ?? ??? ??? ? ????. ??? ??? ?? ?? ???? ???? ?? ?? ???? ??? ? ????:
<?php header("Content-type:text/html;charset=utf-8"); class customException extends Exception { public function errorMessage() { // 錯(cuò)誤信息 $errorMsg = '錯(cuò)誤行號(hào) '.$this->getLine().' in '.$this->getFile() .': <b>'.$this->getMessage().'</b> 不是一個(gè)合法的 E-Mail 地址'; return $errorMsg; } } $email = "someone@example.com"; try { // 檢測(cè)郵箱 if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) { // 如果是個(gè)不合法的郵箱地址,拋出異常 throw new customException($email); } // 檢測(cè) "example" 是否在郵箱地址中 if(strpos($email, "example") !== FALSE) { throw new Exception("$email 是 example 郵箱"); } } catch (customException $e) { echo $e->errorMessage(); } catch(Exception $e) { echo $e->getMessage(); } ?>
?? ??:
? ??? ? ?? ??? ??????. ?? ? ???? true? ?? ?? ??? ?????.
1. customException() ???? ?? ?? ???? ?????. ??. ?? ???? ?? ?? ???? ?? ??? ???? ?????.
2. errorMessage() ??? ?????. ??? ??? ???? ??? ? ??? ?? ???? ?????.
3. $email ??? ??? ??? ????? "example"??? ???? ??? ???? ?????.
4. "try" ?? ??? ???? ? ?? ????? ??? ???? ????.
5. ???? "example"??? ???? ???? ???? ? ?? ??? ??? ??????.
6. "catch" ?? ??? ??? ???? ??? ?? ???? ?????.
customException ???? ??? ?????? customException? ???? ?? ?? ??? ??? ?? ??? ??? ?????.
Rethrow ??
??? ??? ???? ?? , ??? ??? ???? ?? ?? ????. ??? "catch" ???? ?? ??? ? ????.
????? ????? ??? ??? ??? ???. ??? ??? ??????? ??? ? ??? ???? ?? ??? ????. ???? ? ?? ??? ? ??? ??? ???? ???
??:
<?php header("Content-type:text/html;charset=utf-8"); class customException extends Exception { public function errorMessage() { // 錯(cuò)誤信息 $errorMsg = $this->getMessage().' 不是一個(gè)合法的 E-Mail 地址。'; return $errorMsg; } } $email = "someone@example.com"; try { try { // 檢測(cè) "example" 是否在郵箱地址中 if(strpos($email, "example") !== FALSE) { // 如果是個(gè)不合法的郵箱地址,拋出異常 throw new Exception($email); } } catch(Exception $e) { // 重新拋出異常 throw new customException($email); } } catch (customException $e) { // 顯示自定義信息 echo $e->errorMessage(); } ?>
? ???? ??? ?? ???? ? ????. ?:
? ??? ??? ??? "example"??? ???? ???? ??? ??? ?????. ???? ??? ?? ??????.
1. customException() ???? ?? ?? ???? ???? ?????. ?? ???? ?? ?? ???? ?? ??? ???? ?????.
2. errorMessage() ??? ?????. ??? ??? ???? ??? ? ??? ?? ???? ?????.
3. $email ??? ??? ??? ????? "example"??? ???? ??? ???? ?????.
4. "try" ???? ??? ?? ??? ? ??? ? ?? "try" ??? ???? ????.
5. ???? "example"??? ???? ???? ???? ??? ?????.
6. "catch" ?? ??? ??? ???? "customException"? ?? ??????.
7. "customException"? ???? ?? ???? ?????.
?? "try" ???? ??? ???? ??? ? ?? ??? catch ??? ????.
??? ?? ??? ??
set_Exception_handler() ??? ???? ?? ?? ??? ???? ??? ?? ??? ?????.
<?php header("Content-type:text/html;charset=utf-8"); function myException($exception) { echo "<b>Exception:</b> " , $exception->getMessage(); } set_exception_handler('myException'); throw new Exception('Uncaught Exception occurred'); ?>
? ??? ??? ??? ????.
??: Uncaught Exception ??
? ???? "catch" ??? ??? ??? ?? ???? ??? ????. ????????. ? ??? ???? ?? ?? ??? ???? ? ???? ???.
?? ??
· ?? ??? ??? ??? try ?? ??? ???? ???? ??? ???? ???.
· ? try ?? throw ?? ???? ???? catch ?? ??? ?? ?? ??? ???.
· ??? ??? ??? ????? ?? ?? ?? ??? ?????.
· try ?? ?? catch ???? ??? ??(?? ??)? ? ????.
???, ??? ???? ?? ??? ???.