Java Error Handling Basics-The story that catch is only picked up in the foreground

I'm really wondering where to pick up the error.

If you try-catch in a subroutine, you may not reach the place you want to pick up.

java was the same

Examples that do not reach

If the same Exception is thrown in a multi-stage subroutine, it can only be picked up in the inner subroutine.

public class MyException {
	final private static int[] M = {1,2,3}; 
	public static void main(String[] args) {
		try {
			subroutine(M);
		}
		catch ( ArrayIndexOutOfBoundsException e ) {
		   System.out.println("[SECOND] catch : " + e.getMessage());
		}
	}

	public static void subroutine(int[] M) throws ArrayIndexOutOfBoundsException {
		try {
			System.out.println(M[4]);
		}
		catch ( ArrayIndexOutOfBoundsException e ) {
		   System.out.println("[FIRST] catch : " + e.getMessage());
		}
	}
}

result

[FIRST] catch : Index 4 out of bounds for length 3

Example to arrive

If you throw it again in the inner catch, you can pick it up on the outside. When throwing, pass the argument e in a nice way. If it remains e, an error occurs. I'm not sure.

public class MyException {
	final private static int[] M = {1,2,3}; 
	public static void main(String[] args) {
		try {
			subroutine(M);
		}
		catch ( ArrayIndexOutOfBoundsException e ) {
		   System.out.println("[SECOND] catch : " + e.getMessage());
		}
	}

	public static void subroutine(int[] M) throws ArrayIndexOutOfBoundsException {
		try {
			System.out.println(M[4]);
		}
		catch ( ArrayIndexOutOfBoundsException e ) {
		   System.out.println("[FIRST] catch : " + e.getMessage());
		   throw new ArrayIndexOutOfBoundsException(e.getMessage());
        <---Throw
		}
	}
}

result

[FIRST] catch : Index 4 out of bounds for length 3
[SECOND] catch : Index 4 out of bounds for length 3

So what I mean is --It is easier to see where to pick up errors as much as possible ――But each module also has a role, so it is important to divide it within the scope of responsibility. --When delimiting, if you easily pick up Exception / RUntimeException, it will not reach the outside --You have to pick up a proper xxException as finely as possible inside. ――In order to do that, you need to know exactly what to do and what exception to come out. --If & throw is the best -(like if == 0: throw new DivededByZeroException)

reference http://math.hws.edu/javanotes/c8/s3.html

The last code here also consists of if + throw. I think I'm writing something like that, but I'm too sleepy to understand anymore, so I go to bed.

Well that kind of memo

Recommended Posts

Java Error Handling Basics-The story that catch is only picked up in the foreground
The story that .java is also built in Unity 2018
[Docker] The story that an error occurred in docker-compose up
Error logging and exception handling that you can't often see in the Java area
[Rails] About the error that the image is not displayed in the production environment
The story that the Servlet could not be loaded in the Java Web application
The story of writing Java in Emacs
The story of an Illegal State Exception in Jetty.
[Docker] The story that an error occurred in docker-compose up
[Java] Where is the implementation class of annotation that exists in Bean Validation?
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
A story about the JDK in the Java 11 era
The intersection type introduced in Java 10 is amazing (?)
The story of learning Java in the first programming
The story that link_to is deep (cause unknown)
[Java] Something is displayed as "-0.0" in the output
The story that the variable initialization method called in the Java constructor should not be overridden
A small story that is sometimes useful in Maven
Which is better, Kotlin or Java in the future?
Code that only displays the built-in camera in Processing
[Error] The app is not displayed in the production environment
The story that led to solving the error because postgres did not start with docker-compose up
wsimport error handling (A class / interface with the same name "xxx" is already in use)