程序中异常捕获的实际运用?

WBOY
Release: 2016-06-06 20:34:55
Original
1215 people have browsed it

程序中的捕获异常 在什么情况下使用?
是否抛出异常还是需要自己在程序中判断,那如果判断程序中的值不对 我直接调用自己的函数来处理就行了。为什么还需要放在异常里面处理?
新手:-D 请大家指点我一下。

回复内容:

程序中的捕获异常 在什么情况下使用?
是否抛出异常还是需要自己在程序中判断,那如果判断程序中的值不对 我直接调用自己的函数来处理就行了。为什么还需要放在异常里面处理?
新手:-D 请大家指点我一下。

  • 返回值可以被忽略,违背“早崩溃”原则
  • 返回值不带有详细的调试信息(backtrace等)
  • 大量返回值/错误码 分类需要通过丑陋的数组、in_array、switch等实现,Exception可以继承,天然具备分类机制
  • 返回值需要调用过程中的每一层级都做正确的处理(即使只是透明往外抛),而异常可以只关心抛异常的那一层和最终处理异常的那一层,中间的层级完全无感知
Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!