当前位置:首页 > CN2资讯 > 正文内容

PHP Exceptionphp throw new exception

1天前CN2资讯

PHP Exception

官方文档:http://cn2.php.net/exceptions

Deom 1

<?php class Conf { private $file; private $xml; private $lastmatch; function __construct( $file ) { $this->file = $file; if ( ! file_exists( $file ) ) { throw new Exception( "file '$file' does not exist" ); } $this->xml = simplexml_load_file($file); } function write() { if ( ! is_writeable( $this->file ) ) { throw new Exception("file '{$this->file}' is not writeable"); } file_put_contents( $this->file, $this->xml->asXML() ); } function get( $str ) { $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]"); if ( count( $matches ) ) { $this->lastmatch = $matches[0]; return (string)$matches[0]; } return null; } function set( $key, $value ) { if ( ! is_null( $this->get( $key ) ) ) { $this->lastmatch[0]=$value; return; } $conf = $this->xml->conf; $this->xml->addChild('item', $value)->addAttribute( 'name', $key ); } } try { //$conf = new Conf( dirname(__FILE__)."/conf01.xml" ); //$conf = new Conf( dirname(__FILE__)."/conf.unwriteable.xml" ); $conf = new Conf( "nonexistent/not_there.xml" ); print "user: ".$conf->get('user')."\n"; print "host: ".$conf->get('host')."\n"; $conf->set("pass", "newpass"); $conf->write(); } catch ( Exception $e ) { die( $e->__toString() ); } ?>

Deom 2

<?php class XmlException extends Exception { private $error; function __construct( LibXmlError $error ) { $shortfile = basename( $error->file ); print_r( $error ); $msg = "[{$shortfile}, line {$error->line}, col {$error->column}] {$error->message}"; $this->error = $error; parent::__construct( $msg, $error->code ); } function getLibXmlError() { return $this->error; } } class FileException extends Exception { } class ConfException extends Exception { } class Conf { private $file; private $xml; private $lastmatch; function __construct( $file ) { $this->file = $file; if ( ! file_exists( $file ) ) { throw new FileException( "file '$file' does not exist" ); } $this->xml = simplexml_load_file($file, null, LIBXML_NOERROR ); if ( ! is_object( $this->xml ) ) { throw new XmlException( libxml_get_last_error() ); } $matches = $this->xml->xpath("/conf"); if ( ! count( $matches ) ) { throw new ConfException( "could not find root element: conf" ); } } function write() { if ( ! is_writeable( $this->file ) ) { throw new Exception("file '{$this->file}' is not writeable"); } file_put_contents( $this->file, $this->xml->asXML() ); } function get( $str ) { $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]"); if ( count( $matches ) ) { $this->lastmatch = $matches[0]; return (string)$matches[0]; } return null; } function set( $key, $value ) { if ( ! is_null( $this->get( $key ) ) ) { $this->lastmatch[0]=$value; return; } $conf = $this->xml->conf; $this->xml->addChild('item', $value)->addAttribute( 'name', $key ); } } class Runner { static function init() { try { $conf = new Conf( dirname(__FILE__)."/conf.broken.xml" ); print "user: ".$conf->get('user')."\n"; print "host: ".$conf->get('host')."\n"; $conf->set("pass", "newpass"); $conf->write(); } catch ( FileException $e ) { // permissions issue or non-existent file throw $e; } catch ( XmlException $e ) { // broken xml } catch ( ConfException $e ) { // wrong kind of XML file } catch ( Exception $e ) { // backstop: should not be called } } } Runner::init(); ?>

申明

非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

博文欢迎转载,但请给出原文连接。

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/26187.html

    标签: PHPException
    分享给朋友:
    返回列表

    上一篇:php ?? ?:理解

    下一篇:php rewrite

    “PHP Exceptionphp throw new exception” 的相关文章

    远程VPS优选指南:高效管理虚拟专用服务器的最佳实践

    随着远程工作的普及和数字化转型的加速,远程VPS(虚拟专用服务器)逐渐成为许多企业和个人的首选工具。VPS通过虚拟化技术,让我们能够在一台物理服务器上同时运行多个独立的操作系统,这种灵活性使得用户能够像管理独立服务器那样,远程登录和管理自己的虚拟环境。每天都有更多的人意识到,拥有一个VPS可以为他们...

    注册域名的全面指南:选择合适域名的重要性和流程

    在当今数字化的时代,注册域名对企业和个人来说都是至关重要的。域名不仅是一个网站的名字,更是一个在网络空间中的唯一标识符。它能帮助用户快速记住你的品牌,提升他们对你的信任度。如果没有一个合适的域名,用户可能会在茫茫互联网中迷失方向,导致潜在客户流失。 我个人在选择域名时,充分意识到其独特的价值。一个好...

    提升跨境业务体验:CN2GIA全球互联网接入服务解析

    在当今数字化时代,网络服务的质量直接影响着企业的发展。CN2GIA,即“Global Internet Access”,是中国电信为了提升国际网络服务而推出的一项高端业务。为了满足不断增长的国际市场需求,CN2GIA 的出现标志着中国电信在建设下一代网络上的重要一步。它的目标是为用户提供更优质的国际...

    Debian 修改 DNS 的详细步骤与常见问题解决方案

    在讨论 Debian 中的 DNS 修改前,我想先和大家分享一些关于 DNS 的基本信息。DNS(Domain Name System)是互联网的“电话簿”,它将我们可读的网站地址(如 www.example.com)转换为计算机能够理解的 IP 地址。这一过程对于我们浏览网页、发送邮件等操作至关重...

    AT&T VPS详解:稳定性与灵活性的完美选择

    在这篇文章中,我想和大家聊聊AT&T VPS,尤其是在更广泛的虚拟私有服务器市场中,AT&T VPS代表了什么。AT&T是一家美国知名的电信运营商,其提供的VPS服务在一定程度上依赖于它的网络基础设施。这种结合了高质量网络与虚拟服务器技术的服务,吸引了大量需要高稳定性和可扩展...

    AWS永久免費的最佳服務與注意事項

    谈到云服务,AWS是一个令人兴奋的名字,它致力于让用户体验到强大的云计算能力。AWS提供的永久免费服务,吸引了很多企业和开发者的关注。这些免费服务不仅帮助初创公司和小型企业节省成本,还为个人学习和项目开发提供了极好的平台。 在AWS的体系中,Free Tier是一项特别的服务,允许新用户在规定的时间...