1. 程式人生 > 實用技巧 >Java自定義異常的用法

Java自定義異常的用法

 1 package day162020072701.day1601;
 2 
 3 /**
 4  * @authorliuwenlong
 5  * @create2020-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  * @authorliuwenlong
 5  * @create2020-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  * @authorliuwenlong
 7  * @create2020-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 }