[Java] About try-catch exception handling

Programming study diary

November 5, 2020 I used the try-catch statement in Java, so I will summarize how to use it.

What is exception handling?

First, I will explain what kind of state exception handling is. When I look it up, it comes out as follows. (Source)

A program that should not be stopped has failed due to some error If a problem occurs on the way, I want to control the subsequent processing myself

There are two ways to deal with exceptions: a method to resolve the error on the spot (try-catch statement), and a method that does not deal with the exception on the spot and suspends processing and leaves it to the caller (method thorws clause). There is a way. The Java try-catch statement tries to see if an exception occurs in the program (try), catches the exception if it occurs (catch), and does something.

What is a try-catch statement?

As mentioned above, it is used for processing that may cause an exception. By using the try-catch statement, the processing when an exception does not occur and the processing when an exception occurs can be separated. By using finally, it is possible to describe the process that is always executed at the end regardless of the presence or absence of an exception.

How to write

try {
  //Processing that may cause an exception
} catch (Exception type argument) {
  //What to do if an exception occurs(Processing that is not performed unless an exception occurs)
} finally {
  //The last process that is always executed, with or without exceptions

Exception variable names are free, but many write them as e </ b>.

Sample code

As an example, write an exception program that occurs when reading a file.

try {
 FileReader fr = new FileReader("test.txt"); 
} catch (FileNotFoundException e) { 
 System.out.println("No file"); 

Sample code that intentionally raises an exception

try {
  if(i == 0){
     throw new ArithmeticException("Intentionally raise an exception");
  int mul = i * 5;
  return mul
} catch (Exception e) {
  System.out.println("Exception occurs");
  return 0;

Execution result when i is 0

Intentionally raise an exception
java.lang.ArithmeticException:Exception occurs
Return value= 0

Exception class type

Exception class Explanation Example
java.lang.IllegalArgumentException Inappropriate arguments Argument specification error
java.lang.IllegalStateException Illegal state Called uninitialized
java.lang.NullPointerException Null access String value is Null
java.lang.IndexOutOfBoundsException Out of range Array index number is over
java.lang.ArithmeticException Illegal arithmetic calculation Division by zero
java.lang.NumberFormatException Conversion to invalid numeric type The conversion source string is not a number
java.io.FileNotFoundException I can't open the file The file does not exist
java.io.IOException I / O illegal The file does not exist


Use class java.lang.Exception (https://docs.oracle.com/javame/config/cdc/ref-impl/cdc1.1.2/jsr218/ja/java/lang/class-use/Exception.html ) Basic Java try-catch statement! Learn better error handling methods [Java] Let's implement exception handling with try-catch! How to use the Exception class

Recommended Posts

[Java] About try-catch exception handling
Java exception handling?
About exception handling
About exception handling
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
[For Java beginners] About exception handling
About Ruby exception handling
Questions in java exception handling throw and try-catch
[Java] Practice of exception handling [Exception]
Java exception handling usage rules
Exception handling techniques in Java
Exception handling
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
Exception handling Exception
[In-house study session] Java exception handling (2017/04/26)
Step-by-step understanding of Java exception handling
About Java interface
[Java] About Java 12 features
ruby exception handling
[Java] About arrays
[Java] Exception instance
Something about java
Where about java
About Java features
Ruby exception handling
About Java threads
[Java] About interface
About Java class
About Java arrays
Java (exception handling, threading, collection, file IO)
About java inheritance
About interface, java interface
[Introduction to Java] About exception handling (try-catch-finally, checked exception, unchecked exception, throws, throw)
About List [Java]
About java var
About Java literals
About Java commands
try-catch-finally exception handling How to use java
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
Exception handling practice (ArithmeticException)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Ruby] Exception handling basics
[Java] About Singleton Class
[java] throw an exception
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
Spring Boot exception handling
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers