Java笔记--通过异常处理错误

假使精神不跟着堕落,那么他可以战胜一切艰难。

                                                                                                                -- 但丁《神曲》

一、异常的概念

Java程序编译或者运行遇到的错误

Java中的异常分为3种:

        Throwable:

        Error:错误

        Exception:

                编译时期异常

                RuntimeException(运行时期异常)

Java遇到程序问题时,默认的处理方案是:结束Java进程,报错。

然而,异常处理方案分为两种:

        1、try...catch...finally

        2、throws

方案1:

try{

        可能会出错的代码;

}catch(异常类,参数){

        处理的方案逻辑;

}

try...catch...注意事项:

  1. try中定义变量作用域就在try种使用
  2. 若try中代码没有异常,是不会匹配catch的
  3. catch若没有try出现的异常类型的话,就会使用Java默认处理异常方案
  4. catch可以写多个,也可以写一个,但是若多个catch存在继承关系的话,将父亲放在儿子的后面
  5. 若使用jdk1.8新特性写法,使用一个catch接收多个异常类的话,
package day15;


/*
异常的处理方案:
        1、try...catch...finally
        2、throws

    方案1:
        try{
            可能会出错的代码;
        }catch(异常类 参数){
            处理的方案逻辑;
        }

    try..catch..注意事项:
        1、try中定义变量作用域就在try种使用
        2、若try中代码没有异常,是不会匹配catch的
        3、catch若没有try出现的异常类型的话,就会使用java默认处理异常方案
        4、catch可以写多个,也可写一个,但是若多个catch存在继承关系的话,将父亲放在儿子的后面
        5、若使用jdk1.8新特性写法,使用一个catch接收多个异常类的话,不允许出现父子关系异常
 */
public class ExceptionDemo1 {
    public static void main(String[] args) {
//        try{//方式一
//            int[] arr = {11,22,33};
//            System.out.println(arr[2]);
//            int a = 10;
//            System.out.println(a/0);
//        }catch (ArrayIndexOutOfBoundsException e){
//            System.out.println("取不了该取的索引。。。");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误。。。。");
//        }
//
//        try{//方式二
//            int[] arr = {11,22,33};
//            System.out.println(arr[2]);
//            int a = 10;
//            System.out.println(a/0);//ArithmeticException --> new ArithmeticException()
//        }catch (ArithmeticException e){
//            System.out.println("除0错误。。。。");
//        }catch(Exception e){//Exception e = new Xxxx()
//            System.out.println("程序出现错误。。。。");
//        }
//
//        try{//方式三
//            int[] arr = {11,22,33};
//            System.out.println(arr[34]);//ArrayIndexOutOfBoundsException
//            int a = 10;
//            System.out.println(a/0);//ArithmeticException --> new ArithmeticException()
//        }catch (ArithmeticException | ArrayIndexOutOfBoundsException e){
//            System.out.println("出现问题。。。。");
//        }
//        System.out.println(arr[1]);

//        try {//错误的方式
//            int[] arr = {11,22,33};
//            System.out.println(arr[1]);//ArrayIndexOutOfBoundsException
//            int a = 10;
//            System.out.println(a/0);//ArithmeticException --> new ArithmeticException()
//        }catch (Exception e){
//            System.out.println("程序出现错误。。。。");
//        }catch (ArithmeticException e){
//            System.out.println("除0错误。。。。");
//        }

        try {
            int a = 10;
            System.out.println(a / 0);  // new ArithmeticException()
        } catch (ArithmeticException e) {// ArithmeticException e = new ArithmeticException()
            System.out.println(e.getMessage());
            System.out.println(e.toString());
//            e.printStackTrace();
        }

        System.out.println("hello world");//报错的跳过,后面的还可以运行

    }
}

finally 关键字

格式用法

try {

//内容

}catch{

//内容

}finally{

//内容,无论try代码中是否发生问题这里面的内容都会运行,一般情况下,这里面放的都是释放资源的代码。

}

package day15;

public class ExceptionDemo2 {
    public static void main(String[] args) {
        try {
            int a = 10;
            System.out.println(a / 0);  // new ArithmeticException()
        } catch (ArithmeticException e) {// ArithmeticException e = new ArithmeticException()
//            System.out.println(e.getMessage());
//            System.out.println(e.toString());
            e.printStackTrace();
        }finally {//无论try中的代码是否出现问题,这里都会执行
            //一般情况下,finally中里面写释放资源的代码
            System.out.println("好好学习,天天向上:");
        }

        System.out.println("hello world");//报错的跳过,后面的还可以运行
    }
}

方案2:

异常处理的第二种方案:throws

在方法的定义小括号后面加异常类名,可以同时抛出多个异常类,表示该方法可能会出现某个异常,表示的是一种可能性。
throw关键字:在方法的内部使用,后面跟着一个具体的异常对象,表示一定会发生某个异常。
throws抛出的若是编译时期的异常,调用者必须要进行处理,否则程序无法通过遍历。
若抛出的是运行时期的异常,可以不处理,可以运行。
package day15;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*

       异常处理的第二种方案:throws

       在方法的定义小括号后面加异常类名,可以同时抛出多个异常类,表示该方法可能会出现某个异常,表示的是一种可能性

       throw关键字:在方法的内部使用,后面跟着一个具体的异常对象,表示一定会发生某个异常。

       throws抛出的若是编译时期的异常,调用者必须要进行处理,否则程序无法通过遍历。
       若抛出的是运行时期的异常,可以不处理,可以运行
 */
public class ExceptionDemo3 {
    public static void main(String[] args) {
//        try{
//            fun1();
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//
//        System.out.println("okk" +
//                "okk");

        fun1();
        try{
            fun2();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("66666666666666");
        }
        System.out.println("555555555555");

    }

    public static  void fun2() throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("xxx");
    }

    public static void fun1() throws ArrayIndexOutOfBoundsException{
        int[] arr = {1,2,3,4};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的索引: ");
        int index = sc.nextInt();
        if(index >= 0 && index <= arr.length - 1){
            System.out.println(arr[index]);
        }else{
            throw new ArrayIndexOutOfBoundsException();
        }

        System.out.println("世界和平,人民幸福");
    }
}
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值