By GokiSoft.com| 19:54 13/02/2023|
Java Basic

[Souce Code] Tìm hiểu Exception - C2206L

#AgeException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson07;

/**
 *
 * @author diepvan
 */
public class AgeException extends Exception{
    
}

#EmailException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson07;

/**
 *
 * @author diepvan
 */
public class EmailException extends Exception{
    
}

#FullnameException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson07;

/**
 *
 * @author diepvan
 */
public class FullnameException extends Exception{
    
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson07;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
        try {
            Student std = new Student("Tran Van A", 22, "a@gmail.com");
            System.out.println(std);
        } catch (AgeException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EmailException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FullnameException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

#Student.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson07;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author diepvan
 */
public class Student {
    String fullname;
    int age;
    String email;

    public Student(String fullname, int age, String email) throws AgeException, EmailException, FullnameException {
        Pattern pattern = Pattern.compile("^[a-zA-Z ]{6,50}$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher("Visit W3Schools!");
        if(!matcher.find()) {
            throw new FullnameException();
        }
        
        this.fullname = fullname;
        if(age <= 0 || age > 120) {
            //TH sai -> die program
            throw new AgeException();
        }
        this.age = age;
        
        if(!email.contains("@")) {
            throw new EmailException();
        }
        this.email = email;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public int getAge() {
        return age;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", age=" + age + ", email=" + email + '}';
    }
    
    
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó