为什么java到处都要用到try catch?
天蓬老师
天蓬老师 2017-04-18 10:26:06
0
18
1835

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(18)
小葫芦

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

Peter_Zhu

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

黄舟

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.

大家讲道理

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.

Ty80

Cooperate with logs to quickly locate errors and error messages

Peter_Zhu

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

刘奇

Catching exceptions is beneficial to program stability

大家讲道理

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.

Peter_Zhu

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. 🎜
巴扎黑

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)

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