新手学java,在什么情况下要包括try catch啊?
我理解的try catch 是处理异常。
难道java有些方法对象 本身就要配合try catch来使用的吗?
高洛峰2017-04-18 10:28:06
You need to try catch because the method declaration of the object you use may throw an exception.
天蓬老师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
黄舟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.
大家讲道理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.
天蓬老师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
大家讲道理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.
天蓬老师2017-04-18 10:28:06
The Java language is very robust. Non-RuntimeException
must be 非RuntimeException
必须要在程序中捕获或向上抛出,总之总是要处理。try catch
caught
try catch
is used to handle exceptions. 🎜巴扎黑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)