day05--Java基础知识--Exception异常

package day05;

/*
 * 异常:在程序运行时发生了不正常的情况,此时由系统中断程序的运行
 * 
 * 异常类的由来:当程序发生不正常情况时,Java中提供一个类描述这个不正常情况的信息,
 *   包含异常信息、异常类名、异常的位置
 *   
 *异常分类:
 *  Error 错误:   由系统原因造成,一般是系统资源分配冲突时或系统崩溃等原因
 *              这一类Error错误,对于程序员来说是无法处理的
 *
 *  Exception:  由于程序原因造成的,一般是运算、I/O读取等原因
 *              这一类Exception异常,可以进行处理
 *
 *  Java中异常类的体系:
 *  
 *      Throwable
 *          Error:
 *          Exception:
 *              RuntimeException:非受控异常(运行时异常)
 *
 *  在Java中如何处理异常
 *
 *      1.捕获异常处理
 *          格式1:
 *          try{
 *              可能出现异常的语句;
 *              }catch(异常类型 对象名){
 *                  处理异常语句;
 *              }
 */

public class Demo3 {
    static int divide(int a, int b) {
        int result = 0;
        try {
            result = a / b;
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("除数为0了!");
        }
        System.out.println("计算结果完成...");
        return result;// 当此处出现异常时,会抛给调用者(main方法)
    }

    public static void main(String[] args) {
        // 从命令行中读取运行此程序的两个参数
        String aStr = args[0];
        String bStr = args[1];

        // 将数字字符串转成数值类型
        int a = Integer.parseInt(aStr);
        int b = Integer.parseInt(bStr);

        int r = divide(a, b);// 在运行时会发生ArithmeticException,则main抛给JVM
        System.out.println(r);
    }

}
package day05;

/*
 * 异常:在程序运行时发生了不正常的情况,此时由系统中断程序的运行
 * 
 * 异常类的由来:当程序发生不正常情况时,Java中提供一个类描述这个不正常情况的信息,
 *   包含异常信息、异常类名、异常的位置
 *   
 *异常分类:
 *  Error 错误:   由系统原因造成,一般是系统资源分配冲突时或系统崩溃等原因
 *              这一类Error错误,对于程序员来说是无法处理的
 *
 *  Exception:  由于程序原因造成的,一般是运算、I/O读取等原因
 *              这一类Exception异常,可以进行处理
 *
 *  Java中异常类的体系:
 *  
 *      Throwable
 *          Error:
 *          Exception:
 *              RuntimeException:非受控异常(运行时异常)
 *
 *  在Java中如何处理异常
 *
 *      1.捕获异常处理
 *          格式1:
 *          try{
 *              可能出现异常的语句;
 *              }catch(异常类型 对象名){
 *                  处理异常语句;
 *              }
 *      2.抛出异常
 *      1)声明方法时抛出异常
 *          方法名() throws 异常类名[,异常名,异常类名]{}
 *      2)在方法中抛出异常
 */

public class Demo3 {
    static int divide(int a, int b) throws ArithmeticException {
        int result = 0;
        result = a / b;// 当发生异常时,向调用者抛出异常

        // 当异常抛出时,其下的语句不会被执行
        System.out.println("计算结果完成...");
        return result;// 当此处出现异常时,会抛给调用者(main方法)
    }

    public static void main(String[] args) {
        // 从命令行中读取运行此程序的两个参数
        String aStr = args[0];
        String bStr = args[1];

        // 将数字字符串转成数值类型
        int a = Integer.parseInt(aStr);
        int b = Integer.parseInt(bStr);
        try {
            int r = divide(a, b);// 在运行时会发生ArithmeticException,则main抛给JVM
            System.out.println(r);
        } catch (Exception e) {//可以处理所有异常,即Exception类是所有异常的父类
            e.printStackTrace();
        }

    }

}
package day05;

/*
 * 处理多个异常的格式:
 *  try{
 *  }catch(异常类名 对象名){
 *  }catch(...){
 *  }catch(...){
 *  }
 *  
 *  注意:处理多个异常类时,异常的子类所在的catch语句要放在最上面
 */

public class Demo5 {
    static class Person {
        String name;

        public Person(String name) {
            this.name = name;
        }

        public String toString() {
            return "[name=" + name + "]";
        }
    }

    static void sub(int a, int b, int[] nums, Person p) {

        try {
            int r = a / b;//此行如果出现了异常,则下行代码不会执行
            r = nums[5] / 9;
            System.out.println(p);
        } catch (ArithmeticException e) {
            System.out.println("除数为0了...");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组的索引值越界了...");
        } catch (NullPointerException e) {
            System.out.println("访问的对象是null");

        }
    }

    public static void main(String[] args) {

        int[] nums = { 9, 10, 8, 66, 22, 109 };
        sub(10, 2, nums, new Person("张三"));

        sub(10, 0, new int[3], null);
    }

}
package day05;

import javax.management.RuntimeErrorException;

/*
 * 抛出多个异常的格式:
 *      
 *      在方法声明的后边,使用 throws异常类型列表  语句
 */

public class Demo6 {
    static class Person {
        String name;

        public Person(String name) {
            this.name = name;
        }

        public String toString() {
            return "[name=" + name + "]";
        }
    }

    // 抛出多个异常,抛给调用者处理
    static void sub(int a, int b, int[] nums, Person p)
            throws ArithmeticException, IndexOutOfBoundsException,
            NullPointerException {

        int r = a / b;// 此行如果出现了异常,则下行代码不会执行
        r = nums[5] / 9;
        System.out.println(p);
    }

    static void div(int a, int b) {
        if (b == 0) {
            // 抛出异常
            throw new RuntimeException("除数为0");
        }
    }

    public static void main(String[] args) {

        int[] nums = { 9, 10, 8, 66, 22, 109 };
        // sub(10, 2, nums, new Person("张三"));

        try {
            //sub(10, 4, new int[3], null);
            div(9, 0);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组长度小于6");
        } catch (IndexOutOfBoundsException e) {// 父类异常的处理放在子类异常处理的下面
            System.out.println("索引值越界异常...");
        } catch (Exception e) {// 异常类的父类所处的catch要放在最后
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值