There are three types of Exceptions. (1) Error exception (no need to catch) ② Exception type exception (must be caught) ③ RuntimeException type exception (no need to catch)
(2) Exception-type exceptions (checked exceptions) will result in a compile error unless they are caught.
The program crashes due to an error while reading or writing a file, and the file remains open. In that case, the try ~ catch ~ finally statement is effective. try { // Original processing } catch (Exception e) { // Exception handling } finally { // Processing that always works }
Exception propagation Call the sub method from the main method Suppose there is a process to call the subsub method from the sub method. If an Exception occurs in the subsub method Unless caught, it will be fooled by the calling method. This is called exception propagation. However, in the case of Exception type exception, if it is not caught, a compile error will occur, so Exception propagation does not occur, but exception propagation is allowed by making a slow declaration when declaring the method. public static void main method (String str) throws IOException { sub(); }
0922 postscript About specific types of Exception-type exceptions (checked exceptions) -IOException (I / O related exception) -SQLException (DB exception) -ClassNotFoundException (class not found) -InstantiationException (when new class that cannot be instantiated) -InterruptedException (interruption to thread) -NoSuchFieldException (the class does not have the requested field) -NoSuchMethodException (the class does not have the requested method)
Recommended Posts