异常处理机制:
发生异常的时候,异常处理机制会接管程序,并开始寻找一个恰当的地方开始执行程序;即要么执行异常处理程序,要么继续运行下去。
public class Exception1 {/*如果在方法内部发生异常,将发生两种情况1,用try 块包裹,方法继续执行*/public static void main(String[] args) {int a = 5, b = 0;//若要执行ArrayIndexOutOfBoundsException 将b换做其他值try {int res = a / b;} catch (ArithmeticException e) {}System.out.println("继续执行");}}//2,进入异常处理程序中public static void main(String[] args) {int a = 5, b = 0;String[] arrays = {"a","b","c"};try {int res = a / b;String temp = arrays[4];} catch (ArithmeticException e) {throw new ArithmeticException("除数不能为0");} catch (ArrayIndexOutOfBoundsException e){//类似于方法得返回值throw new ArrayIndexOutOfBoundsException("数组越界了");} catch (RuntimeException e){//最大得异常得放在后面}System.out.println("继续执行");}
概念理解:可以将异常处理看作是一种不同的返回机制,还能用抛出异常的方式从当前作用域退出
