There are three main types of errors:
-Compile error due to grammatical error ・ Examples are forgetting semicolons, misspellings, etc. -Not compiled
-Errors caused by problems during execution -For example, a file that should be there is deleted, a value that should be there is a null value, etc. -Compiled
-The execution result is different from what was intended due to a syntax error. ・ For example, 3 + 5 is output as 35 with calculator software. -Compiled
There are 3 types ・ ** For "2. Runtime error", write the code assuming the error occurs and deal with it! ** ** ・ ** "1. Grammar error" and "3. Logical error" can be dealt with by modifying the original code! ** **
-Classes are prepared for each error occurrence status, and are composed of inheritance hierarchies. ・ The meaning of each class ・ The inheritance hierarchy diagram is as follows
** For ʻException`, it is necessary to write code that can handle when an error occurs **
Exceptions are listed in Java API
There are two main types of exception handling.
Used when writing the processing when an exception occurs in its own method
try {
Original processing
}catch (Exception class variable name) {
What to do if an exception occurs
}finally{
Process to be executed without fail regardless of exceptions
}
[Processing flow]
(1) Describe the process in the try
method, and if an exception occurs, pass the instance of the error class to catch
(2) Receive an exception instance with (exception class variable name)
of catch (exception class variable name)
.
Describe the processing after receiving in the method.
③ In finally
, processing such as closing / disconnecting the file is performed.
So-called error message. Displayed with String getMessage ()
.
In what order did the JVM call the methods of the program and where did the exception occur?
Display the contents with void printStackTrace
.
public static void main (String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("date.txt");//Instance generation
fw.write("hellow");//Write hello inside
}catch (Exception e) {
System.out.println("Some exception has occurred");
}finally{
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-Catch (Exception e)
means that all exception instances of the descendants of ʻException` will be received.
-See below for the FileWriter class
https://docs.oracle.com/javase/jp/6/api/java/io/FileWriter.html
https://www.sejuku.net/blog/20657
finally
try (
File instance generation
){
Processing for files
}catch{
}
Files and databases opened in () will automatically call the close
method after processing is complete.
How to describe the processing when an exception occurs when asking the calling method
Method name throws exception class name 1,Exception class name 2
[Supplement]
-By declaring throws
, the description of try catch statement
is no longer obligatory.
-Caller method needs to write try catch statement
It is possible not only to receive the exception that occurred, but also to notify the exception
throw Exception instance name
//main method
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setAge(-100);
}
}
//Person class
public class Person {
int age;
public void setAge(int age) {
if(age<0) {
throw new IllegalArgumentException("Age should be a positive number.");
}
this.age = age;
}
}
//Output result
Exception in thread "main" java.lang.IllegalArgumentException:Age should be a positive number.
at Java15.Person.setAge(Person.java:7)
at Java15.Main.main(Main.java:6)
-See below for the IllegalArgumentException class https://docs.oracle.com/javase/jp/7/api/
Recommended Posts