Je me demande vraiment où trouver l'erreur.
Si vous essayez de rattraper dans un sous-programme, vous risquez de ne pas atteindre l'endroit que vous souhaitez récupérer.
java était le même
Si la même exception est appliquée dans plusieurs sous-programmes, elle ne peut être récupérée que dans le sous-programme interne.
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
Si vous le relancez dans la prise intérieure, vous pouvez le ramasser à l'extérieur. Lors du lancer, passez l'argument e d'une manière agréable. S'il reste e, une erreur se produit. Je ne suis pas sûr.
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());
        <---Jeter
		}
	}
}
result
[FIRST] catch : Index 4 out of bounds for length 3
[SECOND] catch : Index 4 out of bounds for length 3
Donc ce que je veux dire c'est
reference http://math.hws.edu/javanotes/c8/s3.html
Le dernier code ici consiste également en if + throw. Je pense que j'écris quelque chose comme ça, mais j'ai trop sommeil pour comprendre, alors je vais me coucher.
Eh bien ce genre de mémo
Recommended Posts