动手动脑

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();
}
}


__EOF__

本文作者258333
本文链接https://www.cnblogs.com/258-333/p/17775860.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   258333  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示