Java 异常 -- 自定异常 -- 多线程

该文展示了如何在Java中自定义异常,如AgeOutOfRangeException和PriceOutOfRangeExcepion,以及在Person类和Student类中使用这些异常处理年龄和分数超出范围的情况。此外,还介绍了多线程的基本用法,创建了两个线程分别执行不同的任务,如播放背景音乐和显示画面。
摘要由CSDN通过智能技术生成

案例1:自定义年龄异常

需求:
创建一个Person类,提供get/set方法,
在setAge方法中判断年龄是否符合(0-130)这个范围,
如果不符合抛出一个对象,提示:年龄不符合!

a.我们需要自定义异常,来描述年龄不在0-130范围这种情况
建立一个自定义异常类:AgeOutOfRangeException

public class AgeOutOfRangeException extends Exception {
    public AgeOutOfRangeException(){}
    public AgeOutOfRangeException(String message){
        super(message);
    }
}

建立一个Person类

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", 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) throws AgeOutOfRangeException {
        if(age >= 0 && age <= 130){
        this.age = age;
        }else{
            throw new AgeOutOfRangeException("您的年龄" + age + "不符合(0-130)!");

        }
    }
}

建立测试类:Test

public class Test {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("张三");
        try{
        p.setAge(140);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(p);
    }
}

案例2:多线程同时做多件事情

需求:
使用多线成实现同时做多件事情

public class Test {
    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                    System.out.println("打游戏!");
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                    System.out.println("听歌");
                  }
        }).start();
            System.out.println("吃瓜子");
    }
}

习题1

需求:
定义一个方法,功能是:可以获取一个int[]数组中的最大值。请实现此方法,并针对可能出现的异常情况使用throw抛出相应的异常,并给出提示信息。

public class Demo {
    public static void main(String[] args) {
        int[] arr = {};
        if(arr.length <= 0){
            throw new ArrayIndexOutOfBoundsException("数组中没有元素");
        }
        getMax(arr);
    }

    public static void getMax(int[] arr) {
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        System.out.println(max);
    }

}

习题2

需求:
请使用代码实现

  1. 每一个学生(Student)都有学号,姓名和分数,分数永远不能为为负数。
  2. 如果试图给学生赋值一个负数,抛出一个自定异常 。

建立一个异常类:PriceOutOfRangeExcepion

public class PriceOutOfRangeExcepion extends Exception{
    public PriceOutOfRangeExcepion(String message) {
        super(message);
    }

    public PriceOutOfRangeExcepion() {
    }
}

建立一个学生类:Student

public class Student {
    private String id;
    private String name;
    private int price;

    @Override
    public String toString() {
        return "Stuednt{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) throws PriceOutOfRangeExcepion {
        if(price >= 0){
        this.price = price;
        }else{
            throw new PriceOutOfRangeExcepion("你的成绩" + price + "不能小于0");
        }
    }
}

建立一个测试类:Test

public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setId("001");
        stu.setName("张三");
        try {
            stu.setPrice(100);
        } catch (PriceOutOfRangeExcepion priceOutOfRangeExcepion) {
            priceOutOfRangeExcepion.printStackTrace();
        }
        System.out.println(stu);
    }
}

习题3

需求:
编写程序,创建两个线程对象,一根线程输出“播放背景音乐”,另一根线程输出“显示画面”,要求线程实现 Runnable接口。

public class Demo {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                    System.out.println("播放背景音乐");
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                    System.out.println("显示画面");
            }
        }).start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值