Home  >  Q&A  >  body text

java-web - 如何理解java的异常处理机制?

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

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

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

PHP中文网PHP中文网2741 days ago494

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:17:18

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:17:18

    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.

    reply
    0
  • Cancelreply