海阔天空

当前时间为:
欢迎大家来到海阔天空https://www.9713job.com,广告合作以及淘宝商家推广请微信联系15357240395

2020java教程:异常

未分类
2020-08-26 15:40:29
1822677238@qq.com

手机扫码查看

2020java教程:异常

异常

什么是异常?
概念:程序在运行过程中出现的特殊情况。
异常处理的必要性:任何程序都可能存在大量的位置问题、错误,如果不对这些问题进行正确处理,则可能程序中断,造成不必要的损失。

异常的分类
Throwable:可抛的,一切错误或异常的父类,位于java.lang包中
|-Error:JVM、硬件、执行逻辑错误,不能手动处理
|-Exception:程序在运行和配置中产生的问题,可处理。
||-RuntimeException:运行时异常,可处理,可不处理
||-CheckedException:受查异常,必须处理

异常的产生
自动抛出异常:当程序在运行时遇到不符合规范的代码或结果时,会产生异常
手动抛出异常:语法:throw new 异常类型(“实际参数”)
产生异常结果:相当于遇到 return语句,导致程序因异常而终止

异常的传递
异常的传递:按照方法 调用链反向传递,如始终没有处理异常,最终会由JVM进行默认异常处理(堆栈跟踪信息)
受查异常:throws 声明异常,修饰在方法参数列表后端
运行时异常:因可处理可不处理,无序声明异常

public class demos{
    public static void main(String[] args) {
        m1(2);
    }
    public static void m1(int n){
        System.out.println("m1()-start");
        m2(n);
        System.out.println("m1()-end");
    }
    public static void m2(int n){
        System.out.println("m2()-start");
        m3(n);
        System.out.println("m2()-end");
    }
    public static void m3(int n){
        System.out.println("m3()-start");
        if(n%2!=0){
            System.out.println("m3()-end");
        }else{
            throw new RuntimeException();
        }
    }
}

异常的处理

import java.util.*;
public class demos{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=0,b=0,result=0;
        try{
            //可能出现异常的代码
            System.out.print("请输入被除数:");
            a=sc.nextInt();
            System.out.print("请输入除数:");
            b=sc.nextInt();
            result=a/b;
        }catch (ArithmeticException e){
            //异常处理的相关代码,如 e.getMessage()、e.printStackTrace()
            System.out.println("算数异常");
        }catch (InputMismatchException e){
            System.out.println("输入有误");
        }finally {
            //无论是否出现异常,都需执行的代码结构,常用于释放资源
            System.out.println(result);
        }
    }
}

异常处理结构
try{}catch{}
try{}catch{}catch{}
try{}catch{}finally{}
try{}catch{}catch{}finally{}
try{}finally{}
注:多重catch,遵循从子到父的顺序,父类异常在最后

自定义异常
需继承自Exception或Exception子类,常用于RuntimeException
必要提供的构造方法:
无惨构造方法
String message参数的构造方法

import java.util.*;

public class demos{
    public static void main(String[] args) {
        Student s1=new Student();
        Scanner sc=new Scanner(System.in);
        try{
            System.out.print("请输入姓名:");
            String name=sc.next();
            s1.setName(name);
            System.out.print("请输入性别:");
            String sex=sc.next();
            s1.setSex(sex);
            System.out.print("请输入年龄:");
            int age=sc.nextInt();
            s1.setAge(age);
        }catch (InputMismatchException e){
            System.out.println("输入异常");
        }catch (AgeException | SexException e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println(s1);
        }
    }
}
class Student{
    private String name;
    private String sex;
    private Integer age;
    public Student(){}
    public Student(String name, String sex, Integer age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) throws SexException {
        if("男".equals(sex)||"女".equals(sex)){
            this.sex=sex;
        }else{
            throw new SexException("性别有误");
        }
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) throws AgeException {
        if(age>100||age<1){
            throw new AgeException("年龄有误");
        }else this.age=age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}
//性别异常
class SexException extends Exception{
    public SexException() {
    }
    public SexException(String message) {
        super(message);
    }
}
//年龄异常
class AgeException extends Exception{
    public AgeException() {
    }
    public AgeException(String message) {
        super(message);
    }
}

方法覆盖
带有异常声明的方法覆盖:
-方法名、参数列表、返回值类型必须和父类相同
-子类的访问修饰符和父类相同或是比更宽
-子类中的方法,不能抛出比父类更宽的异常

public class demos{
    public static void main(String[] args) {
        Super s=new Sub();
        try {
            s.method();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class Super{
    //父类抛出的异常,子类抛的异常小于等于父类的异常
    public void method() throws Exception{
        System.out.println("Super - method()");
    }
}
class Sub extends Super{
    @Override
    public void method(){
        System.out.println("Super - method()");
    }
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注