search

Home  >  Q&A  >  body text

为什么java到处都要用到try catch?

新手学java,在什么情况下要包括try catch啊?
我理解的try catch 是处理异常。
难道java有些方法对象 本身就要配合try catch来使用的吗?

天蓬老师天蓬老师2802 days ago1804

reply all(18)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:28:06

    You need to try catch because the method declaration of the object you use may throw an exception.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:28:06

    Except for RuntimeException, if other exceptions are not caught, the compilation will fail. Generally, the compiler will prompt that the method may throw an exception

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:28:06

    Sometimes there are many methods that are not executed smoothly all the way to the end, and there will always be mistakes and exceptions reported in the middle. To know what went wrong, you have to catch the exception and handle it in a reasonable way.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:28:06

    Exception capture can quickly locate problems. It is generally added to the code that you think may generate exceptions. Choosing the appropriate Exception and code location is the most critical.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:28:06

    Cooperate with logs to quickly locate errors and error messages

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:28:06

    First of all, try catch is not used everywhere. try catch is used when an exception may be thrown. It is a good mechanism, but don’t abuse it.
    Even some programming languages ​​do not recommend using try catch

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:28:06

    Catching exceptions is beneficial to program stability

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:28:06

    A robust program does not run as we think. It will have some accidents during the running process, such as database connection, calling its method on a null reference, the local file you want to read does not exist, etc. Various unexpected situations, these are exceptions, which must be taken into consideration when writing a program. At this time, you need to catch the exception and then handle it specially.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:28:06

    The Java language is very robust. Non-RuntimeException must be 非RuntimeException必须要在程序中捕获向上抛出,总之总是要处理。try catchcaught

    or 🎜thrown upward🎜 in the program. In short, it must always be handled. try catch is used to handle exceptions. 🎜

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:28:06

    Those who design methods can use throws to declare that a function "may" throw some kind of exception

    Those who use this method must consider this exception (either try/catch when calling, or throw the exception themselves, otherwise a compilation error will occur if neither is done)

    reply
    0
  • Cancelreply