动手动脑

public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}

        throw new ArithmeticException(); 
    } 
    catch(ArithmeticException e) { 
        System.out.println("发生ArithmeticException"); 
    } 
    catch(ArrayIndexOutOfBoundsException e) { 
       System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
    } 
} 

}
image

public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
} catch (ArithmeticException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "try-catch");
}
throw new ArithmeticException();
} catch (ArithmeticException e) {
System.out.println("·¢ÉúArithmeticException");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "2132123aaatry-catch");
}
}
}
上面是同样道理

观测异常处理,catch抓取几个异常

import java.io.*;

public class CheckedExceptionDemo {
public static void main(String[] args) {
try {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in)); // 抛出受控的异常
System.out.print("请输入整数: ");
int input = Integer.parseInt(buf.readLine()); // 有可能引发运行时异常
System.out.println("input x 10 = " + (input * 10));
}
// 以下异常处理语句块是必须的,否则无法通过编译
catch (IOException e) {
System.out.println("I/O错误");
}
// 以下异常处理语句块可以省略,不影响编译,但在运行时出错
catch (NumberFormatException e) {
System.out.println("输入必须为整数");
}
}
}
image
image

查看try-catch先后嵌套顺序

public class EmbededFinally {

public static void main(String args[]) {

	int result;

	try {

		System.out.println("in Level 1");

		try {

			System.out.println("in Level 2");
			// result=100/0; //Level 2

			try {

				System.out.println("in Level 3");

				result = 100 / 0; // Level 3

			}

			catch (Exception e) {

				System.out.println("Level 3:" + e.getClass().toString());

			}

			finally {

				System.out.println("In Level 3 finally");

			}

			// result=100/0; //Level 2

		}

		catch (Exception e) {

			System.out.println("Level 2:" + e.getClass().toString());

		} finally {

			System.out.println("In Level 2 finally");

		}

		// result = 100 / 0; //level 1

	}

	catch (Exception e) {

		System.out.println("Level 1:" + e.getClass().toString());

	}

	finally {

		System.out.println("In Level 1 finally");

	}

}

}
image

浮点数除以0是infinity,不是异常

public class ThrowDemo {
public static void main(String[] args) {
// try {
double data = 100 / 0.0;
System.out.println("浮点数除以零:" + data);
// if(String.valueOf(data).equals("Infinity"))
// {
// System.out.println("In Here" );
// throw new ArithmeticException("除零异常");
// }
// }
// catch(ArithmeticException e) {
// System.out.println(e);
// }
}

```
![image](https://img2023.cnblogs.com/blog/2973495/202310/2973495-20231016225007019-1437710534.png)

import java.io.*;

public class ThrowMultiExceptionsDemo {
public static void main(String[] args) {
try {
throwsTest();
} catch (IOException e) {
System.out.println("捕捉异常");
}
}

private static void throwsTest() throws ArithmeticException, IOException {
System.out.println("这只是一个测试");
// 程序处理过程假设发生异常
throw new IOException();
// throw new ArithmeticException();
}
}

posted @ 2023-10-19 22:28  258333  阅读(25)  评论(0编辑  收藏  举报