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.
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
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.
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)
You need to try catch because the method declaration of the object you use may throw an exception.
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.
Cooperate with logs to quickly locate errors and error messages
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.
The Java language is very robust.
or 🎜thrown upward🎜 in the program. In short, it must always be handled.Non-RuntimeException
must be非RuntimeException
必须要在程序中捕获或向上抛出,总之总是要处理。try catch
caughttry 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)