November 5, 2020 I used the try-catch statement in Java, so I will summarize how to use it.
First, I will explain what kind of state exception handling is. When I look it up, it comes out as follows. (Source)
A program that should not be stopped has failed due to some error If a problem occurs on the way, I want to control the subsequent processing myself
There are two ways to deal with exceptions: a method to resolve the error on the spot (try-catch statement), and a method that does not deal with the exception on the spot and suspends processing and leaves it to the caller (method thorws clause). There is a way. The Java try-catch statement tries to see if an exception occurs in the program (try), catches the exception if it occurs (catch), and does something.
As mentioned above, it is used for processing that may cause an exception. By using the try-catch statement, the processing when an exception does not occur and the processing when an exception occurs can be separated. By using finally, it is possible to describe the process that is always executed at the end regardless of the presence or absence of an exception.
How to write
try {
//Processing that may cause an exception
} catch (Exception type argument) {
//What to do if an exception occurs(Processing that is not performed unless an exception occurs)
} finally {
//The last process that is always executed, with or without exceptions
}
Exception variable names are free, but many write them as e
</ b>.
As an example, write an exception program that occurs when reading a file.
try {
FileReader fr = new FileReader("test.txt");
} catch (FileNotFoundException e) {
System.out.println("No file");
}
try {
if(i == 0){
throw new ArithmeticException("Intentionally raise an exception");
}
int mul = i * 5;
return mul
} catch (Exception e) {
System.out.println("Exception occurs");
System.out.println(e);
return 0;
}
Execution result when i is 0
Intentionally raise an exception
java.lang.ArithmeticException:Exception occurs
Return value= 0
Exception class | Explanation | Example |
---|---|---|
java.lang.IllegalArgumentException | Inappropriate arguments | Argument specification error |
java.lang.IllegalStateException | Illegal state | Called uninitialized |
java.lang.NullPointerException | Null access | String value is Null |
java.lang.IndexOutOfBoundsException | Out of range | Array index number is over |
java.lang.ArithmeticException | Illegal arithmetic calculation | Division by zero |
java.lang.NumberFormatException | Conversion to invalid numeric type | The conversion source string is not a number |
java.io.FileNotFoundException | I can't open the file | The file does not exist |
java.io.IOException | I / O illegal | The file does not exist |
Use class java.lang.Exception (https://docs.oracle.com/javame/config/cdc/ref-impl/cdc1.1.2/jsr218/ja/java/lang/class-use/Exception.html ) Basic Java try-catch statement! Learn better error handling methods [Java] Let's implement exception handling with try-catch! How to use the Exception class
Recommended Posts