栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

异常处理(笔记)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

异常处理(笔记)

文章目录
  • 七.异常处理
    • (1)异常概述
    • (2)异常体系结构
    • (3)常见异常
    • (4)异常的处理:抓抛模型
    • (5)异常处理机制1:try-catch-finally
    • (6)异常处理机制2:throws
    • (7)重写方法异常抛出的规则
    • (8)开发中,两种方法的选择
    • (9)手动抛出异常
    • (10)用户自定义异常类

七.异常处理 (1)异常概述

error问题情况:

①:栈溢出:报错情况

②:堆溢出:报错情况

public class ErrorTest {
    public static void main(String[] args){
        //1.栈溢出:报StackOverflowError
        //main(args);
        //2.堆溢出:报OutOfMemoryError
        //Integer[] arr=new Integer[1024*1024*1024];
    }
}
(2)异常体系结构
java.lang.Throwable
	java.lang.Error:一般不编写针对性的代码进行处理
	java.lang.Exception:可以进行异常的处理
         编译时异常:(checked):
		运行时异常:(unchecked)
(3)常见异常
 *      ************************运行时异常********************************
 * 1.NullPointerException:空指针异常
 *
 *         int[] arr=null;
 *         System.out.println(arr[3]);
 *
 *         String str="abc";
 *         str=null;
 *         System.out.println(str.charAt(3));
 *
 * 2.ArrayIndexOutOfBoundsException:数组角标越界
 *          int[] arr=new int[10];
 *          System.out.println(arr[10]);
 *
 *          String str="abc";
 *          System.out.println(str.charAt(3));
 *
 * 3.ClassCastException:类型转换异常
 *          Object obj=new  Date();
 *          String str=(String)obj;
 *
 * 4.NumberFormatException:数字格式异常
 *          String str="123";
 *          str="abc";
 *          int num=Integer.parseInt(str);
 *
 * 5.InputMismatchException:输入不匹配
 *          Scanner scanner=new Scanner(System.in);
 *          int score=scanner.nextInt();
 *          System.out.println(score);
 *
 * 6.ArithmeticException:算数异常
 *          int a=10;
 *          int b=0;
 *          System.out.println(a/b);
(4)异常的处理:抓抛模型

过程一:“抛”:程序正在执行时,一旦出现异常,会在异常代码处生成一个对应异常类的对象。并将此对象抛出
一旦抛出对象以后,其后的代码就不再执行

过程二:“抓”:可以理解为异常的处理方式:①try-catch-finally ②throws

(5)异常处理机制1:try-catch-finally
public class ExceptionTest{
    public void test1(){
        String str="123";
         str="abc";
         int num=0;//如果try结构外要使用,现在try外声明(需要赋值,万一try中未赋值上,相当于没初始化)
         try {
              num = Integer.parseInt(str);//出现异常
             //出现异常后续代码不执行,所以hello------1不执行
             System.out.println("hello------1");

             //不是错误的类型,不执行
         }catch(NullPointerException e){
             System.out.println("出现空指针异常,不要着急......");
         }catch(NumberFormatException e){
             System.out.println("出现数值转换异常,不要着急......");//处理异常
             //①:String getMessage()
             System.out.println(e.getMessage());
             //②:printStackTrace()
             e.printStackTrace();
        }
         //try外使用num
         System.out.println(num);

         //处理完成继续执行后续代码,这里hello------2是执行的
         System.out.println("hello------2");
    }
}

编译时异常处理例子:

①:使用try-catch-finally处理编译异常时,使得程序在编译时就不再报错,但是运行时仍可能报错
相当于使用try-catch-finally将一个编译时可能出现的异常,延迟到运行时出现

②:开发中,由于运行时异常很常见,所以我们通常就不针对运行时异常编写try-catch-finally
针对于编译时异常,我们说一定要考虑异常的处理

try-catch-finally中finally的使用:
1.finally是可选的
2.finally中声明的一定是可执行代码,即使catch中又出现异常,try中有return语句,catch有return语句等情况
3.像数据库连接,输入输出流,网络编程Socket等资源,JVM是不能自动回收的,我们需要自己手动进行资源的 释放。此时的资源释放的操作就要放在finally中。

public int method(){
    try{
        int[] arr=new int[10];
        System.out.println(arr[10]);
        //上面出异常,此return不执行,则需要在在下方也写一个return
        return 1;
    }catch (ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
        //这里也需要return
        return 2;
    }finally {
        System.out.println("我一定会被执行");
        //如果finally中有return则返回finally的值(体现出一定被执行)
        return 3;
    }
}
(6)异常处理机制2:throws
public class ExceptionTest2 {
    public static void main(String[] args){
//        try {
//            method2();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        method3();
    }

    //可以写一个方法来处理异常,处理完毕可以直接在main中调用
    public static void method3(){
        try {
            method2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void method2()throws IOException{
        method1();
    }

    public static void method1() throws FileNotFoundException, IOException {
        File file =new File("hello.txt");
        FileInputStream fis=new FileInputStream(file);
        int data= fis.read();
        while(data!=-1){
            System.out.println((char)data);
            data=fis.read();
        }
        fis.close();
    }
}
(7)重写方法异常抛出的规则
public class OverrideTest {
    public void display(SuperClass s1){
        try {
            s1.method();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class SuperClass{
    public void method() throws IOException{

    }
}

class SubClass extends SuperClass{
    //重写方法throws的异常类型比父类中的小,Exception权限大,所以不可以
    public void method() throws FileNotFoundException {

    }
}
(8)开发中,两种方法的选择

1.如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类中重写 的方法中有异常,必须使用try-catch-finally方式处理

2.执行的方法a中,先后又调用了另外几个方法,这几个方法是递进关系执行的。我们建议这几个方法用throws的方式 进行处理。而执行的方法a可以考虑使用try-catch-finally方式进行处理

(9)手动抛出异常

关于异常对象的产生:①:系统自动生成的异常对象
②:手动生成一个异常对象,并抛出throw(并非throws)

public class StudentTest {
    public static void main(String[] args){
        try {
            Student s1=new Student();
            s1.regist(-1001);
            System.out.println(s1);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }


}
class Student{  
    private int id;

    public  void regist(int id) throws Exception{
        if(id>0){
            this.id=id;
        }else{
//            System.out.println("输入数据非法!");
            //手动抛出一个异常对象
//            throw new RuntimeException("输入数据非法!");//运行时异常,只在运行时出异常
            throw new Exception("输入数据非法!");//编译时异常,需要编译前进行处理
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}
(10)用户自定义异常类
public class MyException extends RuntimeException {
    static final long serialVersionUID = -7034897190745766939L;

    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }
}

public  void regist(int id) throws Exception{
        if(id>0){
            this.id=id;
        }else{
//            System.out.println("输入数据非法!");
            //手动抛出一个异常对象
//            throw new RuntimeException("输入数据非法!");//运行时异常,只在运行时出异常
//            throw new Exception("输入数据非法!");//编译时异常,需要编译前进行处理
            //在要抛异常的地方使用
              throw new MyException("不能输入负数");
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号