错误处理
错误处理
抛出异常
可以使用throw
和Exception
抛出异常,这会导致当前方法的执行被终止,并将错误处理的责任转会回给调用的代码
代码清单
public function __construct(string $file)
{
$this->file = $file;
if(!file_exists($file)){
throw new \Exception("file 'file' does not exits");
}
$this->xml = simplexml_load_file($file);
}
try {
$conf = new Conf(__DIR__."/conf10.xml");
print "user:".$conf->get(('user'))."<br>";
print "host:".$conf->get(('host'))."<br>";
}catch (\Exception $e){
die($e->__toString());
}
子类化异常
可以通过继承Exception
类,用户可以自己创建异常类。
为什么这样做?:
- 扩展异常类的功能
- 子类定义的心的异常类型能帮我们处理错误
class FileException extends \Exception{
private $error;
public function __construct(\libXmlError $error)
{
$shorfile = basename($error->file);
$msg = "[{$shorfile},line{$error->file},col{$error->column}] {$error->message}";
$this->error = $error;
parent::__construct($msg,$error->code);
}
public function getLibXmlError(){
return $this->error;
}
}
class XmlException extends \Exception{
private $error;
public function __construct($error)
{
$shorfile = basename($error->file);
$msg = "[{$shorfile},line{$error->file},col{$error->column}] {$error->message}";
$this->error = $error;
parent::__construct($msg,$error->code);
}
public function getLibXmlError(){
return $this->error;
}
}
class ConfException extends \Exception{
//略
}
当SimpleXml遇到一个损坏的xml文件的时候,会生成LibXmlError类。它与EXception
类似,有一个$message
和$codes
属性,利用这种相似性,我在XmlException
中使用LibXmlError
对象。
所有代码
<?php
/**
* Created by PhpStorm.
* User: mali@mali
* Date: 2020/8/3
* Time: 11:39 上午
*/
/**
* Created by PhpStorm.
* User: mali@mali
* Date: 2020/8/3
* Time: 11:09 上午
*/
class FileException extends \Exception{
private $error;
public function __construct($error)
{
$shorfile = basename($error->file);
$msg = "[{$shorfile},line{$error->file},col{$error->column}] {$error->message}";
$this->error = $error;
parent::__construct($msg,$error->code);
}
public function getLibXmlError(){
return $this->error;
}
}
class XmlException extends \Exception{
private $error;
public function __construct($error)
{
$shorfile = basename($error->file);
$msg = "[{$shorfile},line{$error->file},col{$error->column}] {$error->message}";
$this->error = $error;
parent::__construct($msg,$error->code);
}
public function getLibXmlError(){
return $this->error;
}
}
class ConfException extends \Exception{
//略
}
class Conf
{
private $file;
private $xml;
private $lastmatch;
public function __construct(string $file)
{
$this->file = $file;
if (!file_exists($file)) {
throw new FileException("file 'file' does not exits");
}
$this->xml = simplexml_load_file($file);
if (!is_object($this->xml)) {
throw new XmlException("could not find root element:conf");
}
$matches = $this->xml->xpath("conf");
if (!count($matches)) {
throw new ConfException("could not find root element:conf");
}
}
public function write()
{
if (!file_exists($this->file)) {
throw new \Exception("file '$this->file' does not exits");
}
file_put_contents($this->file, $this->xml->asXML());
}
public function get(string $str)
{
$matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
if (count($matches)) {
$this->lastmatch = $matches[0];
return (string)$matches[0];
}
return 0;
}
public function set(string $key, string $vale)
{
if (!is_null($this->get($key))) {
$this->lastmatch[0] = $vale;
return;
}
$conf = $this->xml->conf;
$this->xml->addChild('item', $vale)->addAttribute('name', $key);
}
}
try {
$conf = new Conf(__DIR__ . "/conf1011.xml");
print "user:" . $conf->get(('user')) . "<br>";
print "host:" . $conf->get(('host')) . "<br>";
} catch (FileException $e) {
//文件权限问题,或者文件不存在
}catch (XmlException $e){
//XML 文件损坏
}catch (ConfException $e){
// XML 格式错误
}catch (Exception $e){
echo "备用捕捉器";
}
finally完成清理工作
catch
总是在匹配的异常中才会执行,但是finally
无论在任何时候都会执行
<?php
/**
* Created by PhpStorm.
* User: mali@mali
* Date: 2020/8/3
* Time: 11:39 上午
*/
/**
* Created by PhpStorm.
* User: mali@mali
* Date: 2020/8/3
* Time: 11:09 上午
*/
class FileException extends \Exception{
private $error;
public function __construct($error)
{
$shorfile = basename($error->file);
$msg = "[{$shorfile},line{$error->file},col{$error->column}] {$error->message}";
$this->error = $error;
parent::__construct($msg,$error->code);
}
public function getLibXmlError(){
return $this->error;
}
}
class XmlException extends \Exception{
//略
}
class ConfException extends \Exception{
//略
}
class Conf
{
private $file;
private $xml;
private $lastmatch;
public function __construct(string $file)
{
$this->file = $file;
if (!file_exists($file)) {
throw new FileException("file 'file' does not exits");
}
$this->xml = simplexml_load_file($file);
if (!is_object($this->xml)) {
throw new XmlException("could not find root element:conf");
}
$matches = $this->xml->xpath("conf");
if (!count($matches)) {
throw new ConfException("could not find root element:conf");
}
}
public function write()
{
if (!file_exists($this->file)) {
throw new \Exception("file '$this->file' does not exits");
}
file_put_contents($this->file, $this->xml->asXML());
}
public function get(string $str)
{
$matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
if (count($matches)) {
$this->lastmatch = $matches[0];
return (string)$matches[0];
}
return 0;
}
public function set(string $key, string $vale)
{
if (!is_null($this->get($key))) {
$this->lastmatch[0] = $vale;
return;
}
$conf = $this->xml->conf;
$this->xml->addChild('item', $vale)->addAttribute('name', $key);
}
}
try {
$fh = fopen(__DIR__."/log.txt","a");
$conf = new Conf(__DIR__ . "/conf1011.xml");
print "user:" . $conf->get(('user')) . "<br>";
print "host:" . $conf->get(('host')) . "<br>";
} catch (FileException $e) {
//文件权限问题,或者文件不存在
}catch (XmlException $e){
//XML 文件损坏
}catch (ConfException $e){
// XML 格式错误
}catch (Exception $e){
echo "备用捕捉器";
} finally {
fputs($fh,"end\n");
}