php Try Catch exception testing_PHP tutorial

WBOY
Release: 2016-07-21 15:48:15
Original
1374 people have browsed it

C1 of c is used in the page try catch, b1 of b is used in c1, and a1 of a is used in b1.

The default is: throw an exception in a1, catch the exception of a1 in b1, then throw the exception just now, c1 catches it, then throws it, and finally the page captures and outputs it.
The result is:
X-Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:workspacemyzCollectiontest.php(16): a->a1()
#1 D:workspacemyzCollectiontest.php(28): b->b1()
#2 D:workspacemyzCollectiontest.php(37): c->c1()
#3 C:Program FilesZendZendStudio-5.2.0binphp5dummy.php(1): include('D:workspacemy...')
# 4 {main}end

Second test:
Remove throw $e in b1, it will not be thrown.
The result is:
X-Powered-By: PHP/5.1.1
Content-type: text/html

end

The third test:
Turn on throw new Exception($e->getMessage()); in b1.
Throw a new exception, so that calls above b1 will not get the exception of a1.
The result is:
X-Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:workspacemyzCollectiontest.php(28): b->b1()
#1 D:workspacemyzCollectiontest.php(37): c->c1()
#2 C:Program FilesZendZendStudio-5.2.0binphp5dummy.php(1): include('D:workspacemy...')
#3 {main}end

The fourth test:
Remove all the try catch throw in b1.
Result: Everything is normal, that is to say, the intermediate steps do not need to be thrown, and the top layer can also get the exception thrown by the bottom layer.
There is just one problem. If there is an exception in b, there is no way to get it. If you need to detect b, then you must also add try catch in b
X -Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:workspacemyzCollectiontest.php(16): a->a1()
#1 D:workspacemyzCollectiontest.php(28): b->b1()
#2 D:workspacemyzCollectiontest.php(37): c->c1()
#3 C:Program FilesZendZendStudio-5.2.0binphp5dummy.php(1): include('D:workspacemy...')
# 4 {main}end

class a {
public function a1 () {
try {
throw new Exception('123');
} catch (Exception $e) {
throw $e;
}
}
}

class b {
public function b1 () {
try {
$a = new a();
$a->a1();
} catch (Exception $e) {
throw $e;
//throw new Exception($e->getMessage());
}
}
}

class c {
public function c1 () {
try {
$a = new b();
$a->b1();
} catch (Exception $e) {
throw $e;
}
}
}

try {
$c = new c();
$c->c1();
} catch (Exception $e) {
echo $e->getTraceAsString( );
}
echo 789;

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319773.htmlTechArticlePage try catch uses c1 of c, c1 uses b1 of b, and b1 uses a1 of a. The default is: throw an exception in a1, catch the exception of a1 in b1, then throw the exception just now, catch it in c1,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template