這篇文章主要介紹了PHP實現的簡單異常處理類,結合具體執行個體形式分析了php基於物件導向技術實現異常處理操作的相關實現技巧,需要的朋友可以參考下
具體如下:
<?phpheader('content-type:text/html;charset=UTF-8');// 建立email異常處理類class emailException extends exception{}// 建立pwd異常處理類class pwdException extends exception{ public function __tostring(){ return $this->getMessage().'in file:'.$this->getFile().'on line:'.$this->getLine(); }}function reg($reginfo = null){ // 依據不同錯誤拋出不同異常 if (empty($reginfo) || !isset($reginfo)) { throw new Exception('參數非法'); } if (empty($reginfo['email'])) { throw new emailException('郵件為空白'); } if ($reginfo['pwd'] != $reginfo['repwd']) { throw new pwdException('兩次密碼不一致!'); }}// 接收不同異常,並針對性處理!try { reg(array('email' => '1078789950@qq.com', 'pwd' => '123', 'repwd' => '1231' ));} catch (Exception $e) { echo $e ->getMessage();} catch (emailException $ee) { echo $ee ->getMessage();} catch (pwdException $ep) { echo $ep;}
以上就是本文的全部內容,希望對大家的學習有所協助。