java 异常处理是什么?

异常的种类

异常处理
Throwable (异常处理父类)

Error 系统崩溃 数据库崩溃(跟咱们代码没关系)

Exception 异常(是跟咱们代码有关系的)

RuntimeException 运行时异常

如何解决

出现异常 系统如何解决?(谁来解决)

main函数解决方式

1.main函数自己解决

2.把问题跑出上级去解决(谁调用的main 谁去解决)

交给jvm虚拟机解决 给你报错

打印错误信息 错误发生在什么类的 什么位置

举3个常见的异常

public static void main(String[] args) {
        /*
         * 需求 
         * 1.空指针异常
         * 2.角标越界异常
         * 3.算术异常
         */
        ArrayList<String> arrayList = new ArrayList<>();
        Iterator<String> iterator = arrayList.iterator();


       // 角标越界
//      System.out.println(arrayList.get(2));
//       
        //算术异常
//      System.out.println(2/0);
        //空指针异常(访问一块不属于你的内存空间)
        arrayList = null;
        System.out.println(arrayList.get(2));

    }

异常解决方式

try….catch …. finally
try 指测试这个异常

catch 指捕获这个异常

finally 指异常结束后做的事

捕获异常流程

1.发生异常

2.根据发生的异常 产生 对应的异常对象

3.这个 异常对象 会返回给调用者

4.如果调用者处理了这个异常(添加try…catch)

异常对象会跟catch进行匹配 匹配上执行catch中语句

程序会继续执行

5.如果调用者没有处理这个异常 默认交给jvm去处理

根据 产生异常对象 打印对应错误信息

程序停止

具体过程

public class Demo02 {
    public static void main(String[] args) {
        TestException testException  = new TestException();
        try {
            //可能发生异常的代码
            int num = testException.fun(10,0);
            // 这块反馈了一个对象
            // new ArithmeticException("/by zero")
            System.out.println(num);
        }catch (ArithmeticException e) {
            //捕获异常该咋办
            // catch 如何就能捕获异常
            // ArithmeticException e = new ArithmeticException("/by zero")
            // catch 会匹配这个异常对象
            // 如果匹配上了 就执行 catch 中的语句
            // 捕获异常后 程序会继续执行
            System.out.println("吃药了没 ?除数为0");
        }
                System.out.println("你猜我执行了吗");
    }

}
class TestException{
    //报异常方法
    public int fun(int a , int b) {
        /*
         * 这里发生了算术异常
         * 相当于产生了一个异常对象
         * new ArithmeticException("/by zero")
         * 一旦发生异常 系统会产生一个对应的异常对象
         * 发生异常后 谁用我 这个异常就给谁
         * 处理不了 就给jvm虚拟机处理
         *  打印错误信息(根据异常对象的类型 去打印错误信息
         *  )
         *  然后打印完 会结束你的程序
         * 
         */
        return a/b;
    }
}

多异常

多catch的 捕获异常

异常处理的作用 :帮你来完善代码 让你出现的错误 更容易找到

java

安卓开发 客户端开发 处理异常 一般直接用 Exception处理

javaEE开发

服务器开发 处理异常 打印错误信息 你带吗的错误会被隐藏掉

一般会做日志处理(记录程序被访问的日志) log4j

public static void main(String[] args) {
        int[] array = {1,2,3,4};
        try {
            //测试代码异常

            System.out.println(10/0);
            // 产生 new ArithmeticException()
            // 匹配
            // 匹配成功后 程序继续向下执行
            System.out.println(array[10]);
        }catch (ArithmeticException e) {
            /*
             * 注意:如果使用Exception 直接匹配异常
             * 多个catch 同时捕获时需要写在最后
             * 写前面后面的catch相当于白加了
             */
            System.out.println("病了治 除数为0");
        }catch (Exception e) {
            //如果发生多个异常 需要匹配 多个Catch

            System.out.println("你出错了");
        }

        System.out.println("猜猜我执行了吗");
    }

finally

finally(遗言)

记住 不管你异常有没有发生 有没有被匹配到都会执行

finally 有什么作用?

可以关闭 系统资源避免资源浪费

(列如 关闭 输入流 和关闭数据库)

final finally finalize 有什么区别

雷锋和雷锋塔 没什么练习

final 修饰类 修饰方法 修饰属性

finally 中语句一定会执行(异常处理的语句)

finalize 基类Object类中的方法

所有java对象中都有这个方法

该方法 在垃圾回收时候 被系统自动调用

当一块内存空间 无人引用 这块内存就是垃圾

    public static void main(String[] args) {
        try {
            System.out.println(10/1);
            return;
        } catch (ArithmeticException e) {
            System.out.println("除0了");

        }finally {
            System.out.println("你看我执行了吗");
        }
        System.out.println("我是下面的语句"); 

    }

运行异常 和 编译异常的区别

运行时异常 和编译时异常

除了运行时异常 就是编译时异常

运行时异常—-代码问题(程序员反的错)

出现编译时异常 —–问题的提前预警 强制你去处理

不处理 编译不通过

    public static void main(String[] args) throws FileNotFoundException {
        // 编译异常
        // 系统这时候 不知道你到底有没有这个文件
        // 相当于系统问你 要是没有这个文件怎么办
        // 系统这时会强制 要求你对这个问题作出解释
        // 这就是编译时异常
        // 编译时异常相当于 对问题的一种提前准备
        /*
         * 解决异常有两种方式
         * 1.自己处理 try。。。。catch(想让程序继续执行就 try)
         * 2.把异常跑出去(谁调用 我 跑给谁 谁去处理)
         * (先让程序遇到错误就停止 就抛)
         */

//      try {
//          FileInputStream fs  =  new FileInputStream("wl.txt");
//      } catch (Exception e) {
//          System.out.println("读入文件没找到");
//      }


    }

自定义异常方法

// 创建一个自定义 异常类
// 在创建异常类 时 需要直观让人知道这是什么异常
// 所以类名 要见名知意
class ageOutOfBoundsException extends Exception{
    /**
     * 序列化 时 需要 id
     */
    //重写构造方法
    public ageOutOfBoundsException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ageOutOfBoundsException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }


}

public class Person {
    private String name;
    private int age;
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    // 如果这个方法 要抛出异常 给调用者
    // 那么 必须在方法声明上标识这个方法 为异常方法
    // throws Exception 标识是抛出什么异常
    /*
     * throw 和 throw 区别
     * throws标识 这个方法 可以抛出一个异常
     * (在方法的声明出现)
     * throw 抛出一个异常对象(方法中出现)
     */
    public void setAge(int age) throws Exception {
        if(age>0&&age<120) {
        this.age = age;
        }else {
//          System.out.println("还是人吗");
            // 抛出一个异常 告诉他 你赋值的年龄不对
            // throw  关键词 跑出一个异常对象
            // 创建一个异常对象
//          Exception exception = new Exception("年龄非法");
            // 把这个异常对象抛出去
            // 抛给谁 谁用就跑给谁
            // 使用匿名对象抛出
            //使用自定义类处理异常
            ageOutOfBoundsException e = new ageOutOfBoundsException("年龄非法");
            throw e ;
        }
    }
    public Person(String name, int age) {
        super();

        this.name = name;
        this.age = age;



    }
    public Person() {
        super();
    }



}

Throwable中的方法

      Exception e = new  Exception("HAHA ");
        //调用 异常类中toString 方法
        // 打印的异常类名 和异常信息
        System.out.println(e);
        // 只打印异常信息
        System.out.println(e.getMessage());
        // 打印的时异常类名 信息加出现的位置
        // jvm虚拟机 默认打印的就是这个方法
        e.printStackTrace();

例题

无线输入整数 存放到集合中 打印 输入quit停止

希望在输入字符串的时候 让程序爷能继续运行

public static void main(String[] args) {


        ArrayList<Integer> arrayList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        String num="";
        while (true) {
         num = scanner.next();

            if(num.equals("quit")) {
                break;
            }
          try {
              int num1 =Integer.parseInt(num);
              arrayList.add(num1);
            } catch (NumberFormatException e) {
                System.out.println("请输入数字 或者 quit");

            }



        }
        for (Integer string : arrayList) {
            System.out.println(string);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值