Java自定义异常的用法

复制代码
 1 package day162020072701.day1601;
 2 
 3 /**
 4  * @author liuwenlong
 5  * @create 2020-07-27 09:25:44
 6  */
 7 @SuppressWarnings("all")
 8 public class Human {
 9     private String name;
10     private char sex;
11     private int age;
12 
13     public int getAge() {
14         return age;
15     }
16 
17     public void setAge(int age) throws HumanException.AgeException {
18         if (age > 0 && age < 150) {
19             this.age = age;
20             return;
21         }
22         throw new HumanException.AgeException("年龄非法");
23 
24     }
25 
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public char getSex() {
35         return sex;
36     }
37 
38     public void setSex(char sex) throws HumanException.SexException {
39         if (sex == '男' || sex == '女') {
40             this.sex = sex;
41             return;
42         }
43         throw new HumanException.SexException("性别有误");
44     }
45 
46     @Override
47     public String toString() {
48         return "Human{" +
49                 "name='" + name + '\'' +
50                 ", sex=" + sex +
51                 ", age=" + age +
52                 '}';
53     }
54 }
复制代码
复制代码
 1 package day162020072701.day1601;
 2 
 3 /**
 4  * @author liuwenlong
 5  * @create 2020-07-27 10:47:58
 6  */
 7 @SuppressWarnings("all")
 8 public class HumanException  {
 9     public static class  AgeException extends Exception{
10         private String message;
11 
12         public AgeException(String message) {
13             this.message = message;
14         }
15 
16         @Override
17         public String getMessage() {
18             return message;
19         }
20     }
21 
22     public static class  SexException extends Exception{
23         private String message;
24 
25         public SexException(String message) {
26             this.message = message;
27         }
28 
29         @Override
30         public String getMessage() {
31             return message;
32         }
33     }
34 }
复制代码
复制代码
 1 package day162020072701.day1601;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * @author liuwenlong
 7  * @create 2020-07-27 09:29:24
 8  */
 9 @SuppressWarnings("all")
10 public class TestHuman {
11     public static void main(String[] args) {
12         Scanner input = new Scanner(System.in);
13 
14         Human human = new Human();
15         human.setName("张三");
16         System.out.println("姓名:" + human.getName());
17 
18         boolean flag = false;
19         while (!flag) {
20             flag = true;
21             try {
22                 System.out.println("请输入性别:");
23                 human.setSex(input.next().charAt(0));
24                 System.out.println("性别:" + human.getSex());
25                 System.out.println("请输入年龄:");
26                 human.setAge(input.nextInt());
27                 System.out.println("年龄:" + human.getAge());
28             } catch (HumanException.SexException e) {
29                 flag = false;
30                 System.out.println(e.getMessage());
31             } catch (HumanException.AgeException e) {
32                 System.out.println(e.getMessage());
33                 flag = false;
34             } catch (Exception e) {
35                 System.out.println("出现未知错误!Error!");
36                 flag = false;
37             } finally {
38                 System.out.println("异常首尾");
39             }
40         }
41         System.out.println(human);
42     }
43 }
复制代码

 

posted @   勤快的懒羊羊  阅读(194)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示