[Java] Practice of exception handling [Exception]

Target

For beginners. The content is at the level of "What is exception handling?"

Try to generate an error by dividing an integer by zero

Main.java


public class Main {

	public static void main(String[] args) {
		int a = 5;
		int b = 0;
		System.out.println(div(a,b)); //An error occurs here
        System.out.println("Finish");
	}

	static int div(int a, int b) {
		return a / b;
	}

}

You can compile without any compilation errors. And execute.

Execution result


Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.div(Main.java:19)
	at Main.main(Main.java:9)

Processing ends at the line where the error occurs

System.out.println(div(a,b));

In the part

java.lang.ArithmeticException: / by zero

After this error

System.out.println("Finish");

Even though there is a process called, the process ends at the part where the error on the line above occurs.

Since the program has been forcibly terminated, it is necessary to write the process ** "If an error occurs, do this" **. This is exception handling.

What are the exceptions?

It's an error. You can simply think of it as an error. Strictly speaking, it seems that the way of thinking differs depending on the person, so it's okay to think that exception handling is error handling for the time being.

How to write exception handling try ~ catch

Main.java



	public static void main(String[] args) {
		int a = 5;
		int b = 0;
		try {
			System.out.println(div(a,b));
		} catch(Exception e) { //Receive the error as an object called e (object name can be anything)
			System.out.println("Divide by 0!");
			System.out.println(e); //output e
		} finally {
			System.out.println("Finish");
		}
	}

//Calculations that are likely to cause errors. If there is an error, pass the error information to the Exception class.
	static int div(int a, int b) throws Exception {
		return a / b;
	}

}
try{
Error-predicted processing
} catch (Exception e) {
What to do if an error occurs
} finally {
Processing with or without an error
}

When writing a process in which an error is predicted, do not end there, and if an error occurs, perform the process when a partial catch error occurs. With this, the process will move to the end without stopping.

When I actually run it

Divide by 0!//What to do if an error occurs
java.lang.ArithmeticException: / by zero //Error message
Finish//Is running until finally

Recommended Posts

[Java] Practice of exception handling [Exception]
Java exception handling?
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
Step-by-step understanding of Java exception handling
Exception handling practice (ArithmeticException)
[Java] About try-catch exception handling
Java exception handling usage rules
Exception handling techniques in Java
Exception handling
Exception handling Exception
[In-house study session] Java exception handling (2017/04/26)
Handling of time zones using Java
[Note] Handling of Java decimal point
[For Java beginners] About exception handling
About exception handling
About exception handling
ruby exception handling
[Java] Exception instance
java practice part 1
Ruby exception handling
Java (exception handling, threading, collection, file IO)
[Java] Overview of Java
try-catch-finally exception handling How to use java
[Java] Handling of JavaBeans in the method chain
Questions in java exception handling throw and try-catch
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
About Ruby exception handling
NIO.2 review of java
Review of java Shilber
[Ruby] Exception handling basics
[java] throw an exception
java --Unification of comments
Rock-paper-scissors game java practice
Spring Boot exception handling
Scraping practice using Java ②
History of Java annotation
java (merits of polymorphism)
Java8 Stream API practice
Scraping practice using Java ①
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
[Java] Handling of character strings (String class and StringBuilder class)
About the handling of Null
Classes that require exception handling
Java basic learning content 7 (exception)
About an instance of java
Handling of SNMP traps (CentOS 8)
[Java] Mirage-Basic usage of SQL
Java's first exception handling (memories)
[Java] Beginner's understanding of Servlet-②
Practice of binary search method
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
[Practice! 】 Execution of SQL statement