Java中异常相关案例

一、异常案例代码及效果图

1.捕获异常案例

案例:向一个长度为5的整型数组中,使用死循环不断录入整数,当出现数组下标越界异常时,使用try-catch精准捕获该异常,输出数组已满的提示,并跳出循环,在循环外输出:数据录入结束。

package work1;

import java.util.Scanner;

/*1.向一个长度为5的整型数组中,使用死循环不断录入整数,
当出现数组下标越界异常时,使用try-catch精准捕获该异常,
输出数组已满的提示,并跳出循环,在循环外输出:数据录入结束。*/
public class work1 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr=new int[5];
        for (int i = 0; i < 101000; i++) {
    
    
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你要录入的数字:");
            try {
    
    
                arr[i]=sc.nextInt();
            }catch (ArrayIndexOutOfBoundsException e){
    
    
                System.out.println("数组已满");
                break;
            }
        }
        System.out.println("数据录入结束");
    }
}

在这里插入图片描述

2.主动抛出异常

public void fun2() {
    
    
     throw new NullPointerException("空指针异常");  
     System.out.println("hello world");//会报红,提示unreachable statement,该语句不可能被执行
}

嵌套try-catch

@Test
public void fun2()  {
    
    
    try {
    
    
        throw new Exception("非运行时异常,哈哈哈");
    } catch (Exception e) {
    
    
        e.printStackTrace();
        try {
    
    
            throw new ParseException("解析异常,哈哈哈",0);
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
    }finally {
    
    
        try {
    
    
            throw new TimeoutException("超时异常,哈哈哈");
        } catch (TimeoutException e) {
    
    
            e.printStackTrace();
            try {
    
    
                throw new SQLException("SQL异常");
            } catch (SQLException ex) {
    
    
                ex.printStackTrace();
            }
        }
    }
}

代替返回语句

public int  funR1(){
    
    
        try {
    
    
            return 1;
        }catch (Exception e){
    
    
           throw new RuntimeException();
        }
    }
public int  funR2(){
    
    
    if(true) {
    
    
        return 1;
    }else{
    
    
        throw new RuntimeException();
    }
}

3.自定义异常案例(一)

案例:在person类中,年龄的范围是0-120岁,性别只能是男或女。自定义年龄异常类和性别异常类,输出相应异常提示信息。编写测试类,给人物的年龄和性别赋值,如果不符合要求,抛出自定义异常类的异常提示信息。

package work2;

public class AgeException extends Exception {
    
    
    public AgeException(){
    
    }

    public AgeException(String s){
    
    
        super(s);
    }
}

package work2;

public class Person {
    
    
    private int age;
    private char sex;



    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        if(age>=0&&age<=120){
    
    
            this.age = age;
        }else {
    
    
            try {
    
    
                throw new AgeException("年龄超出范围了");
            } catch (AgeException e) {
    
    
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }

    }

    public char getSex() {
    
    
        return sex;
    }

    public void setSex(char sex) {
    
    
        this.sex = sex;
    }
}

package work2;
/*2.在person类中,年龄的范围是0-120岁,性别只能是男或女。
自定义年龄异常类和性别异常类,输出相应异常提示信息。编写测试类,
给人物的年龄和性别赋值,如果不符合要求,抛出自定义异常类的异常提示信息。*/
public class TestP {
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person();
        p.setAge(130);
    }
}

在这里插入图片描述

4.自定义异常案例(二)

案例:定义一个信用卡类(账户,额度),定义一个People类,属性有:姓名,年龄,信用卡;方法有:刷信用卡。在测试类中实现:杨光,22岁,有一张信用卡,账户:123456789000 额度:2000 。 他现在想刷信用卡给女朋友买一个包包,价值3000. 自定义异常, 当额度超支时,抛出提示“信用额度不足,请理性消费!”。

package work3;

public class CardOutOfLimitException extends Exception {
    
    
    public CardOutOfLimitException(String s){
    
    
        super(s);
    }
}

package work3;
/*
* 3.定义一个信用卡类(账户,额度),定义一个People类,
* 属性有:姓名,年龄,信用卡;方法有:刷信用卡。在测试类中实现:
* 杨光,22岁,有一张信用卡,账户:123456789000   额度:2000  。
*  他现在想刷信用卡给女朋友买一个包包,价值3000.
*  自定义异常, 当额度超支时,抛出提示“信用额度不足,请理性消费!”。
*
* */
public class CredictCard {
    
    
    private String id;
    private double limit;

    public String getId() {
    
    
        return id;
    }

    public void setId(String id) {
    
    
        this.id = id;
    }

    public double getLimit() {
    
    
        return limit;
    }

    public void setLimit(double limit) {
    
    
        this.limit = limit;
    }
}

package work3;

public class People {
    
    
    private String name;
    private int age;
    private CredictCard  card;

    public void cusume(double m){
    
    
        if(m<this.card.getLimit()){
    
    
            this.card.setLimit(this.card.getLimit()-m);
        }else {
    
    
            try {
    
    
                throw new CardOutOfLimitException("信用卡额度超支,请理性消费!");
            } catch (CardOutOfLimitException e) {
    
    
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }


    }

    public String getName() {
    
    
        return name;
    }

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

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public CredictCard getCard() {
    
    
        return card;
    }

    public void setCard(CredictCard card) {
    
    
        this.card = card;
    }
}

package work3;

public class TestCard {
    
    
    public static void main(String[] args) {
    
    
        People p = new People();
        p.setName("海哥");
        p.setAge(22);
        CredictCard c = new CredictCard();
        c.setId("666888");
        c.setLimit(2000);
        p.setCard(c);
        p.cusume(3000);

    }
}

在这里插入图片描述

总结

以上就是异常案例的所有内容,主要是三种异常类型的相关案例。

猜你喜欢

转载自blog.csdn.net/StruggleBamboo/article/details/111699078