javase基础11

1.异常的概念

A:异常:就是程序运行中出现的不正常的情况.
B:异常的由来:程序中的问题也是现实生活中的一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象.

2.异常的分类

:异常的继承体系
Throwable
Error (错误:一出现就是致命的)
服务器宕机,数据库崩溃等
Exception
RuntimeException(运行时异常,一般都是程序员犯得错误,需要修改源码的.)
编译时异常:在编译时必须进行处理,不处理无法通过编译.

3.异常的处理方式

try{
  需要检测的异常;
} catch(异常类名 异常变量) {
  异常处理代码
  可以调用异常的方法
  通常我们只使用一个方法:printStackTrace打印异常信息
}

public static void main(String[] args) {
        //指定文件
        Scanner sc = new Scanner(System.in);
        String path = sc.next();
        
        try {
            FileWriter fw = new FileWriter(path);
            System.out.println("输出成功了");
        } catch(IOException e) {
            System.out.println("您输入的路径有误!");
            //用于展示异常出现的位置和基本信息
            e.printStackTrace();
        }
        
        System.out.println("后边的代码");
}

4.使用throws关键字处理异常


throws的方式处理异常
定义功能方法时,需要把出现的问题暴露出来让调用者去处理。
那么就通过throws在方法上标识
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) throws 异常类名{}

/*
 * 运行时异常处理:
 *         该异常在编译期不检查语法错误的.可以处理,也可以不处理.    
 * 
 *         一般情况下,遇到java自身提供的运行时异常,都可以通过逻辑解决,避免异常产生.
 *         常见运行时异常:空指针异常,索引越界异常,类型转换异常
 * 
 * 声明抛出处理
 * 捕获处理
 */
public class Demo1runtime_exception {

    public static void main(String[] args) {
        
        try {
            System.out.println(10/0);
        } catch (ArithmeticException ae) {
//            可以打印异常信息,也可以不打印
//            ae.printStackTrace();
            
//            如果出现异常的话
            System.out.println("除数不能为0!");
        }
        System.out.println("后边的代码");
        
        
        try {
            method();
        } catch (ArithmeticException ae) {
            System.out.println("除数不能为0!");
        }
    }
    
    public static void method() throws ArithmeticException{
        System.out.println(10/1);
    }

}

5.try…catch处理多个异常格式

try{
  需要检测的异常;
} catch(异常类名1 异常变量1) {
  异常处理代码
  可以调用异常的方法
  通常我们只使用一个方法:printStackTrace打印异常信息
} catch(异常类名2 异常变量2) {
  异常处理代码
  可以调用异常的方法
  通常我们只使用一个方法:printStackTrace打印异常信息
}……//可以有无数个catch

6.throws 处理多个异常格式

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) throws 异常类名1,异常类名2……{} 

/*
 * 多异常处理一:
 *         声明抛出处理,直接抛出多个异常即可
 * 
 * 多一场处理二:
 *         一个try多个catch的方式
 * 
 * java是中断异常的处理方式
 */
public class Demo2multi_exception01 {

    public static void main(String[] args) {
        
        //一个try多个catch的方式
        try{
            method();
        }catch(ArithmeticException ae) {
            System.out.println("您的除数不能为0");
        }catch(IOException e) {
            System.out.println("您要访问的文件不存在");
        }
        
        System.out.println("abc");
    }

    //一个方法声明抛出多个异常
    public static void method() throws ArithmeticException,IOException{
        
        System.out.println(10/1);
        System.out.println("method逻辑1完成");
        method2();
        System.out.println("method逻辑2完成");
    }
    public static void method2() throws IOException {
        //指定文件
        Scanner sc = new Scanner(System.in);
        String path = sc.next();
        FileWriter fw = new FileWriter(path);
    }
}

7.多个异常用一个catch处理格式

try{
  需要检测的异常;
} catch(异常类名1|异常类名2…… 异常变量) {
  异常处理代码
  可以调用异常的方法
  通常我们只使用一个方法:printStackTrace打印异常信息
}

8.异常在继承中的注意事项

a:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。(父亲坏了,儿子不能比父亲更坏)
b:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常
c:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws

9.finally关键字

try{
  需要检测的异常;
} catch(异常类名 异常变量) {
  异常处理代码
  可以调用异常的方法
  通常我们只使用一个方法:printStackTrace打印异常信息
}finally{
  此处存放一定要执行的代码
  一般把释放资源的代码放到finally中
}

/*
 * 在try/catch后可以追加finally代码块,其中的代码一定会被执行,通常用于资源回收。
 */
public class Demo1finally {

    public static void main(String[] args) {
        
        //编译时异常
        //"C:/javaSE/11code/myException/a.txt"
        //指定文件
        Scanner sc = new Scanner(System.in);
        String path = sc.next();
        
        try {
            FileWriter fw = new FileWriter(path);
            fw.write("abc");
        } catch (IOException e) {
            System.out.println("产生了IO问题");
        } finally {
            System.out.println("我要关闭资源");
        }
        
        System.out.println("abc");
    }
}

10.自定义异常

Class 异常名 extends Exception{ //或继承RuntimeException
  public 异常名(){
  }
  public 异常名(String s){
    super(s);
  }
}

/*
 * 自定义异常:
 *         编译时异常继承Exception,运行时异常继承RuntimeException。
 *         关键字throw 抛出异常
 *         回忆产生异常:
 *             创建异常对象
 *             抛出异常
 */
public class Democustom_exception {

    public static void main(String[] args) {

        //年龄小于18岁,产生异常
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();
        
        if(age < 18) {
            //产生异常
            //创建异常对象
            U18Exception u18 = new U18Exception("年龄小于18岁");
            //抛出异常
            try {
                throw u18;
            } catch (U18Exception e) {
                System.out.println("年龄不满足婚恋网站年龄");
                e.printStackTrace();
            }
        }
        
        
        //可以直接手动创建异常对象
        try {
            throw new RuntimeException("我产生了一个运行时异常");
        } catch (Exception e) {
            System.out.println("直接创建了一个runtimeexception对象");
        }
        
        System.out.println("abc");
    }

}
原文地址:https://www.cnblogs.com/learnjfm/p/6956501.html