2.4. Exception

简介:
		
$ cat exception1.php
<?php

function test1(){
	throw new Exception("Test1 exception!");
}

try {
	test1();
}
catch(Exception $e){
	die( $e->__toString() );
}


$ php exception1.php
exception 'Exception' with message 'Test1 exception!' in /home/neo/workspace/example/PHP/exception.php:4
Stack trace:
#0 /home/neo/workspace/example/PHP/exception.php(8): test1()
#1 {main}
		
		
		
$ cat exception2.php
<?php

class MyException extends Exception {}
function test2(){
	throw new MyException('Test2 exception');
}
try {
	test2();
}
catch(Exception $e){
	die( $e->__toString() );
}


$ php exception2.php
exception 'MyException' with message 'Test2 exception' in /home/neo/workspace/example/PHP/exception2.php:5
Stack trace:
#0 /home/neo/workspace/example/PHP/exception2.php(8): test2()
#1 {main}
		
		
		
$ cat exception.php
<?php
class FileException extends Exception {}
class DataException extends Exception {}

function test1(){
	throw new FileException("Test1 exception!");
}

function test2(){
	throw new DataException('Test2 exception');
}
function test3(){
	throw new Exception('Test3 exception');
}

try {
	test1();
	test2();
	test3();
}
catch(FileException $e){
    print( $e->__toString() );
}
catch(DataException $e){
    print( $e->__toString() );
}
catch(Exception $e){
	print( $e->__toString() );
}
		
		





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
10月前
|
存储 前端开发 Java
Exception 和 Error
Exception 和 Error
63 0
|
11月前
|
Java API Spring
RuntimeException和Exception的区别
RuntimeException和Exception的区别
98 0
|
Java 程序员 编译器
异常(Exception)
异常(Exception)
76 0
异常(Exception)
|
安全 Java 数据库连接
Java中的异常处理详解(try、catch、finally、throw、throws)
Java中的异常处理详解(try、catch、finally、throw、throws)
257 1
|
Java 编译器 API
RuntimeException和Exception比较
RuntimeException和Exception比较
467 0
|
Linux C# Windows
【C#】简单解决PathTooLong的Exception
原文:【C#】简单解决PathTooLong的Exception 前提 windows系统路径的最大长度限制是260个字符(听说.Net 4.6.2,取消了这个限制),而Linux或者Unix系统的好像是4K个字符。
967 0
|
Java
throws 与 throw
/* * 有些时候,我们是可以对异常进行处理的,但是又有些时候,我们根本就没有权限去处理某个异常。 * 或者说,我处理不了,我就不处理了。 * 为了解决出错问题,Java针对这种情况,就提供了另一种处理方案:抛出。
1052 0