The ** exception class ** provided by the API is in the following inheritance hierarchy.
Here, we will deal with exceptions especially for Exception-type exceptions.
Exception handling can be described in the try-catch
statement.
try{
FileWriter fw = new FileWriter("c:¥¥data.txt");
fw.write("hellow!");
}catch(IOException e){
System.exit();
}
If you do not handle exceptions, the following compilation error will occur in the Exception system.
Exception java.io.IOException is not reported.
You must catch or declare a throw to throw.
(I haven't read it yet)
Enclose the relevant part with try-catch
or
Declare throw IOException
in the method definition and
The caller is required to handle exceptions.
If you write the conditional part of catch like ʻException e`, Catch Exception-type exceptions or RuntimeException-type exceptions. The intention is to roughly catch the offspring class of the Exception class.
try{
FileWriter fw = new FileWriter("c:¥¥data.txt");
fw.write("hellow!");
}catch(Exception e){
System.out.println("Something went wrong"); //Somehow ...
}
I don't know if the phrase "catch abstractly" is correct ...
If you add finally after catch, You can set the process you want to execute regardless of the occurrence of an exception. For cleaning up, such as closing open files and closing database and network connections.
try{
FileWriter fw = new FileWriter("c:¥¥data.txt");
fw.write("hellow!");
}catch(IOException e){
System.out.println("Something went wrong");
}finally{
fw.close();
}
If writing the finally block is a hassle, write ()
in the first try
.
try(
FileWriter fw = new FileWriter("c:¥¥data.txt");
){
fw.write("hellow!");
}catch(IOException e){
System.out.println("Something went wrong");
}
If you go with this, it seems that the first opened data.txt
will be automaticallyclosed ()
.
However, it is limited to types that implement the java.lang.AutoCloseable
interface.
When declaring with a method, write as follows.
** ... method name throws exception class 1, exception class 2, ... **
Sub.java
public class Sub{
public static void subsub() throws IOException{
FileWriter fw = new FileWriter("date.txt");
fw.write("hello!");
}
}
Main.java
public class Main{
public static void main(Strgin[] args){
try{
//There are various processes ...
subsub();
}catch(IOException e){
System.exit();
}
}
}
Because it is not processed by the called subsub method It feels like processing with the main method of the caller.
throws are also used to throw exceptions.
Write as follows. Note that it is singular.
** throw exception instance; **
Or
** throw exception instance ("error message"); **
Person.java
Public class Person{
int age;
public class setAge(int Age){
if(age <= 0){
//If it is 0 or less
throw new IllgalArgumentExeception("The age should be a positive number. Specified value=" + age);
}
//If it is a positive number
this.age = age;
}
}
Main.java
public class Main{
public static void main(String[] args){
Person p = new Person();
p.setAge(-128); //Now try to set the wrong value.
}
}
As an execution result,
Exception in thread "main" java.lang.IllegalArgumentException:The age should be a positive number. Specified value=-128
at Person.setAge(Person.java:6)
at Main.main(Main.java:4)
The throws statement throws an IllegalArgumentException instance to the JVM and reports it. The exception is thrown to the main method, but here the JVM eventually kills the program because it hasn't caught the exception.
You can create your own exception class by inheriting the existing exception class. The method of making is the same as the general inheritance method.
For example, when developing a program to read documents or something I want exception handling when reading an unsupported document file ...
UnsupportedDocumentFileException.java
public class UnsupportedDocumentFileException extends Exception{
public UnsupportedDocumentFileException(String){
//Constructor that receives an error message
super(msg);
}
}
Main.java
public class Main{
public static void main(String[] args){
try{
//Throwing an exception on a trial basis...
throw new UnsupportedDocumentFileException
("This is an unsupported document file");
}catch(Exception e){
e.printStackTrace();
}
}
}
ʻException.printStackTrace () `outputs the following information.
--Class name of the exception thrown --Explanation of the exception passed in the constructor --Caller method and file name / line number
Convenient.
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%8F%E3%81%8B%E3%82%8BJava%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E4%B8%AD%E5%B1%B1-%E6%B8%85%E5%96%AC/dp/484433638X) Pp.562-595
Recommended Posts