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

Java 异常

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

Java 异常

目录
  • 1.自己不处理,交给调用者处理
    • 1.1 throws 声明一个异常
    • 1.2 throw 抛出一个异常
  • 2.自己处理异常
    • 2.1 try...catch
    • 2.2 try...catch的常见问题
  • 3.Throwable 的成员方法
  • 4.自定义异常

异常:就是程序出现了不正常的情况。程序在执行过程中出现的非正常的情况,最终会导致 JVM 的非正常停止。

注意:语法错误不算在异常体系中。

编译时异常:都是 Exception 类及其子类,必须显示处理,否则程序就会发生错误,无法通过编译。

运行时异常:都是 RuntimeException 类及其子类,无需显示处理,也可以和编译时异常一样处理。

如果程序出现了问题,我们没有做任何处理,最终 JVM 会做默认的处理:把异常的名称、异常原因及异常出现的位置等信息输出在控制台,程序停止执行。

package com.qdu.exce;

public class ExceptionDemo2 {
    public static void main(String[] args) {
        int [] arr = {1, 2, 3, 4, 5};
        System.out.println(arr[10]);
        // 虚拟机帮我们创建了一个异常对象,即 new ArrayIndexOutOfBoundsException()
        // 首先看程序中有没有自己处理异常的代码,如果没有,则交给本方法的调用者处理
        // main方法的调用者是虚拟机
        System.out.println("我最帅");
    }
}

1.自己不处理,交给调用者处理 1.1 throws 声明一个异常

格式:throws 异常类名,写在方法的定义处。

告诉调用者:你调用我时,有可能会出现这样的异常哦!如果方法中没有出现异常,那么正常执行;如果方法中真的出现了异常,将这个异常交给调用者处理。

(1) 如果声明的异常是一个运行时异常 RuntimeException,那么声明的代码可以省略。

package com.qdu.exce;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ExceptionDemo6 {
    public static void main(String[] args)  {
        method1();
    }

    private static void method1()  {
        int [] arr = null;
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

(2) 如果声明的异常是一个编译时异常 Exception,那么声明的代码必须要手动写出。

package com.qdu.exce;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ExceptionDemo6 {
    public static void main(String[] args) throws ParseException {
        method2();
    }

    private static void method2() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        sdf.parse("2048-10月10日");
    }
}
1.2 throw 抛出一个异常

格式:throw new 异常();,写在方法内。

package com.qdu.exce;

public class ExceptionDemo7 {
    public static void main(String[] args) {
        System.out.println("家里有一个貌美如花的老婆");
        System.out.println("还有一个当官的兄弟");
        System.out.println("自己还有一个买卖");
        System.out.println("这样的生活你要不要?");
        throw new RuntimeException();
        // 手动创建了一个异常对象,创建之后暂时没有手动处理,抛给了调用者处理
    }
}

在方法中,当传递的参数有误,没有继续运行下去的意义了,则采取抛出处理,表示让该方法结束运行,并告诉调用者方法中出现了问题。

package com.qdu.exce;

public class ExceptionDemo8 {
    public static void main(String[] args) {
        int [] arr = null;
        printArr(arr);
    }

    private static void printArr(int[] arr) {
        if(arr == null){
            throw new NullPointerException();
            // 当参数为null的时候,手动创建了一个异常对象,抛给了调用者
        }else{
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }
}
2.自己处理异常 2.1 try…catch

好处:可以让程序继续往下执行。

package com.qdu.exce;

public class ExceptionDemo9 {
    public static void main(String[] args) {
        int [] arr = null;
        try {
            printArr(arr);
        } catch(NullPointerException e) {
            System.out.println("参数不能为null");
        }
        System.out.println("我最帅!!!");
    }

    private static void printArr(int[] arr) {
        if(arr == null){
            throw new NullPointerException();
        }else{
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }
}
2.2 try…catch的常见问题

(1) 如果 try 中没有遇到问题,怎么执行?

会把 try 中所有的代码全部执行完毕,不会执行 catch 里面的代码。

package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo10 {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的年龄");
            String line = sc.nextLine();
            int age = Integer.parseInt(line); // 格式化异常
            System.out.println(age);
            System.out.println("测试123");
        } catch (NumberFormatException e) {
            System.out.println("格式化异常");
        }
        System.out.println("测试456");
    }
}

(2) 如果 try 中遇到了问题,那么 try 下面的代码还会执行吗?

直接跳转到对应的 catch 语句中,try 下面的代码就不会再执行了,当 catch 里面的语句全部执行完毕,表示整个体系全部执行完,继续执行下面的代码。

package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo10 {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的年龄");
            String line = sc.nextLine();
            int age = Integer.parseInt(line); // 格式化异常
            System.out.println(age);
            System.out.println("测试123");
        } catch (NumberFormatException e) {
            System.out.println("格式化异常");
        }
        System.out.println("测试456");
    }
}

(3) 如果出现的问题没有被捕获,那么程序如何运行?

try…catch 就相当于没有写,默认交给虚拟机处理。

package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo10 {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的年龄");
            String line = sc.nextLine();
            int age = Integer.parseInt(line); // 格式化异常
            System.out.println(age);
            System.out.println("测试123");
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        }
        System.out.println("测试456");
    }
}

(4) 同时有可能出现多个异常怎么处理?

如果出现多个异常,那么就写多个 catch 就可以了。

如果多个异常之间存在子父类关系,那么父类一定要写在下面。

package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo10 {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的年龄");
            String line = sc.nextLine();
            int age = Integer.parseInt(line); // 格式化异常
            System.out.println(age);
            System.out.println(2 / 0); // 数学运算异常
        } catch (NumberFormatException e) {
            System.out.println("格式化异常");
        }catch (ArithmeticException e) {
            System.out.println("数学运算异常");
        }
        System.out.println("测试456");
    }
}

3.Throwable 的成员方法

所有的异常对象都可以调用这三个方法。

package com.qdu.exce;

public class ExceptionDemo11 {
    public static void main(String[] args) {
        try {
            int [] arr = {1, 2, 3, 4, 5};
            System.out.println(arr[10]);
            // 虚拟机帮我们创建了一个异常对象,即 new ArrayIndexOutOfBoundsException();
        } catch (ArrayIndexOutOfBoundsException e) {
            // public String getMessage()   返回此 throwable 的详细消息字符串
            String message = e.getMessage();
            System.out.println(message);
            // Index 10 out of bounds for length 5
        }
    }
}
package com.qdu.exce;

public class ExceptionDemo11 {
    public static void main(String[] args) {
        try {
            int [] arr = {1, 2, 3, 4, 5};
            System.out.println(arr[10]);
            // 虚拟机帮我们创建了一个异常对象,即 new ArrayIndexOutOfBoundsException();
        } catch (ArrayIndexOutOfBoundsException e) {
            // public String toString()     返回此可抛出的简短描述
            String s = e.toString();
            System.out.println(s);
            // java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 5
        }
    }
}
package com.qdu.exce;

public class ExceptionDemo11 {
    public static void main(String[] args) {
        try {
            int [] arr = {1, 2, 3, 4, 5};
            System.out.println(arr[10]);
            // 虚拟机帮我们创建了一个异常对象,即 new ArrayIndexOutOfBoundsException();
        } catch (ArrayIndexOutOfBoundsException e) {
            // public void printStackTrace()    把异常的错误信息输出在控制台(字体为红色的)
            e.printStackTrace();
        }
        System.out.println("嘿嘿嘿");
    }
}

示例:键盘录入学生的姓名和年龄,其中年龄为18-25岁,超出这个范围是异常数据不能赋值需要重新录入,一直录到正确为止。

package com.qdu.exce;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age >= 18 && age <= 25){
            this.age = age;
        }else{
            throw new RuntimeException("年龄超出了范围");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo12 {
    public static void main(String[] args) {
        Student s = new Student();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        s.setName(name);
       while(true){
           System.out.println("请输入年龄");
           String ageStr = sc.nextLine();
           try {
               int age = Integer.parseInt(ageStr);
               s.setAge(age);
               break;
           } catch (NumberFormatException e) {
               System.out.println("请输入一个整数");
               continue;
           } catch (RuntimeException e) {
               System.out.println("请输入一个符合范围的年龄");
               continue;
           }
       }
        System.out.println(s);
    }
}
4.自定义异常

自定义异常是为了让异常信息更加见名知义。

package com.qdu.exce;

public class AgeOutOfBoundsException extends RuntimeException {
    public AgeOutOfBoundsException() {
    }

    public AgeOutOfBoundsException(String message) {
        super(message);
    }
}
package com.qdu.exce;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age >= 18 && age <= 25){
            this.age = age;
        }else{
            throw new AgeOutOfBoundsException("年龄超出了范围");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
package com.qdu.exce;

import java.util.Scanner;

public class ExceptionDemo12 {
    public static void main(String[] args) {
        Student s = new Student();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        s.setName(name);
       while(true){
           System.out.println("请输入年龄");
           String ageStr = sc.nextLine();
           try {
               int age = Integer.parseInt(ageStr);
               s.setAge(age);
               break;
           } catch (NumberFormatException e) {
               System.out.println("请输入一个整数");
               continue;
           } catch (AgeOutOfBoundsException e) {
               System.out.println(e.toString());
               System.out.println("请输入一个符合范围的年龄");
               continue;
           }
       }
        System.out.println(s);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/601302.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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