[JAVA] exception

Exception type

Exception type

There are three main types of errors:

  1. Grammar error
  2. Run-time error
  3. Logical error

1. Grammar error

-Compile error due to grammatical error ・ Examples are forgetting semicolons, misspellings, etc. -Not compiled

2. Run-time error

-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

3. Logical error

-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

Summary···

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 that represent exceptions and their types

-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

スクリーンショット 2018-03-05 9.38.13.png

Summary···

** For ʻException`, it is necessary to write code that can handle when an error occurs **

Handling exceptions

How to check for exceptions

Exceptions are listed in Java API

Example

スクリーンショット 2018-03-05 14.23.18.png

Exception handling type

There are two main types of exception handling.

  1. catch syntax
  2. throws syntax

1. catch syntax

What is the catch syntax?

Used when writing the processing when an exception occurs in its own method

how to use

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.

Information that the exception instance has

① Explanation of exception information

So-called error message. Displayed with String getMessage ().

② Stack trace

In what order did the JVM call the methods of the program and where did the exception occur? Display the contents with void printStackTrace.

Example

	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();
			}
		}
	}

Supplement

-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

Writing without finally

try (
File instance generation
){
Processing for files
}catch{
}

Files and databases opened in () will automatically call the close method after processing is complete.

2. throws syntax

What is the throws syntax?

How to describe the processing when an exception occurs when asking the calling method

how to use

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

How to raise an exception

Way of thinking

It is possible not only to receive the exception that occurred, but also to notify the exception

how to use

throw Exception instance name

Example

//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

exception
Exception handling
Self-made Exception
Exception handling Exception
Java exception handling?
About exception handling
About exception handling
ruby exception handling
Exception switching method
[Java] Exception instance
Ruby exception handling
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
About Ruby exception handling
Exception handling practice (ArithmeticException)
[Ruby] Exception handling basics
[java] throw an exception
Exception when closing try-with-resources
Spring Boot exception handling