java-web - 如何理解java的异常处理机制?
PHP中文网
PHP中文网 2017-04-18 10:15:18
0
2
527

在学习java时,如何看待java的异常处理机制,应不应该把异常看作一个普通的对象?

应不应该把catch看作逻辑正确的完整的一部分?

如何对java的异常处理机制有更深刻的理解以便更加适合的使用它?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
左手右手慢动作

First of all, you need to know 一切皆是对象,异常当然是一个对象。
接下来要知道异常分为受检查异常运行时异常(感谢@泊浮目提醒)。
你所说的异常处理机制应该是受检查异常,受检查异常是可以被java的异常处理机制所处理的,因为他们都是实现了Throwablethis interface in java.

catch is a logically correct and complete part.
Because the caught exception may affect the next action of your method.
For example, when opening a file, you have to make sure the file is open so that you can read and write information.
Catching the exception will let you confirm if the file is not opened, why, and what you need to do next. Create a new file? Or just return without reading or writing information?

Finally, if you want to understand the exception handling mechanism, you still need to read 《java编程思想》this magical book.

左手右手慢动作

What is said upstairs不受检查异常应该叫运行时异常 will be easier to understand. Okay, let’s answer the questions, one by one.

  • Yes.

  • Of course. Also finally is also an important part of the overall logic.

  • Let’s take a look at one of my articles, which explains the anomalies in quite detail.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template