try-catch-finally exception handling How to use java

Environment: Windows10, Eclipse, java8

Describes exception handling using try catch finally in java.

How to write exception handling


try{
processing
}catch(Exception type Argument){
Processing when an exception occurs
}finally{
Last action to be executed(With or without exception)
}

Example of use

	public static void main(String[] args) {
		double a = 99999;

		//----------Cases where exception handling occurs----------
		System.out.println("-----Exception occurred-----");
		try {
			a = 30/0;
		}catch(ArithmeticException e) {
			System.out.println("Exception handling: 0 does not break");
			System.out.println(e);
		}finally {
			System.out.println("finally  "+ "a="+a);
		}

		//----------Case where exception handling does not occur----------
		System.out.println("-----normal-----");
		try {
			a = 30/3;
		}catch(ArithmeticException e) {
			System.out.println("Exception handling: 0 does not break");
			System.out.println(e);
		}finally {
			System.out.println("finally  "+ "a="+a);
		}
	}
Execution result
-----Exception occurred-----
Exception handling: 0 does not break
java.lang.ArithmeticException: / by zero
finally  a=99999.0
-----normal-----
finally  a=10.0

Commentary

In the above program, on the line a = 30/0; An exception (ArithmeticException) has occurred because we are dividing by zero (there is no solution). Therefore, the following processing is executed.

		}catch(ArithmeticException e) {
			System.out.println("Exception handling: 0 does not break");
		}

Therefore, as the execution result, "Exception handling: 0 does not break" is displayed.

About catch (exception type argument)

How should I write the contents of catch?

Example above
}catch(ArithmeticException e){

In the above example, ArithmeticException is an exception that is thrown when divided by zero.

Other exceptions can be written in the following form, so I will explain them.

}catch(Exception type Argument){

Exception type (eg ArithmeticException)

Write the type that will be thrown when an exception occurs.

Check the exception handling corresponding to the processing written in try in the reference and use it. For java11: https://docs.oracle.com/javase/jp/11/docs/api/index-files/index-1.html

With import

Also, if you want to handle exceptions in file I / O operations, etc. When using an exception called IOException

import java.io.IOException;

You may also need to import the package with.

Regarding the exception called ArithmeticException this time,

import java.lang.ArithmeticException

It seems that it is necessary to write and import. However, java.lang is implicitly imported within the compiler and does not need to be written. By the way, this is why System.out.println can be used without import.

Argument (example: e)

You can name it freely. "E" is often used.

In this example Because it describes "System.out.println (e);" "Java.lang.ArithmeticException: / by zero" in the execution result Is displayed, and it can be confirmed that an exception has been received.

Recommended Posts

try-catch-finally exception handling How to use java
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[Rails] How to write exception handling?
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
[Java] How to use Math class
How to use Java enum type
Java exception handling?
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
[Introduction to Java] About exception handling (try-catch-finally, checked exception, unchecked exception, throws, throw)
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes