package cn.tx.exception;
public class ExceptionDemo6 {
public static void main(String[] args) {
int a = 10;
int b = 0;
//try{}catch(){}的快捷键是Shift+Alt+Z
divide(a,b);
}
public static int divide(int a, int b){
int c = 0;
try {
int[] arr = new int[10];
System.out.println(arr[9]);
c = a/b;
} catch (Exception e) {
if(e instanceof ArithmeticException){
System.out.println("处理数学异常");
}else{
System.out.println("数组越界异常");
}
}
return c;
}
}
本文介绍了一个Java异常处理的示例程序,演示了如何捕获并处理算术异常和数组越界异常。通过使用try-catch块,程序能够根据不同类型的异常进行特定的处理,并给出相应的错误提示。
299

被折叠的 条评论
为什么被折叠?



