异常处理机制:
    发生异常的时候,异常处理机制会接管程序,并开始寻找一个恰当的地方开始执行程序;即要么执行异常处理程序,要么继续运行下去。

    1. public class Exception1 {
    2. /*
    3. 如果在方法内部发生异常,将发生两种情况
    4. 1,用try 块包裹,方法继续执行
    5. */
    6. public static void main(String[] args) {
    7. int a = 5, b = 0;//若要执行ArrayIndexOutOfBoundsException 将b换做其他值
    8. try {
    9. int res = a / b;
    10. } catch (ArithmeticException e) {
    11. }
    12. System.out.println("继续执行");
    13. }
    14. }
    15. //2,进入异常处理程序中
    16. public static void main(String[] args) {
    17. int a = 5, b = 0;
    18. String[] arrays = {"a","b","c"};
    19. try {
    20. int res = a / b;
    21. String temp = arrays[4];
    22. } catch (ArithmeticException e) {
    23. throw new ArithmeticException("除数不能为0");
    24. } catch (ArrayIndexOutOfBoundsException e){
    25. //类似于方法得返回值
    26. throw new ArrayIndexOutOfBoundsException("数组越界了");
    27. } catch (RuntimeException e){//最大得异常得放在后面
    28. }
    29. System.out.println("继续执行");
    30. }

    概念理解:可以将异常处理看作是一种不同的返回机制,还能用抛出异常的方式从当前作用域退出