By GokiSoft.com| 10:16 25/09/2021|
Java Advanced

[Share Code] Overview exception trong Java - C2009G - Lập trình Java nâng cao



Nội dung kiến thức:
	- Chữa bài tập 1663 -> CSDL
	- Chữa bài tập 2415 -> Lập trình đa ngôn ngữ
	- Cipher
		- Console
		- Java Swing
	- Exception
===========================================
BT1663
B1. Database + Tables -> CSDL
B2. Project + Add thu vien jdbc mysql driver
B3. Mapping Tables <-> Class Object
B4. Tao DAO

===========================================
md5 -> 1 chieu: A -> md5 -> B: Luu mat khau -> md5(md5(A) + PRIVATE_KEY) -> B2
base64 -> 2 chieu: A -> base64 encode -> B -> base64 decode -> A (a-zA-Z0-9=)
private/public
Cipher: 2 chieu
	A -> cipher encode + pwd -> B -> cipher decode + pwd -> A

===========================================
Exception:
-> AgeException, EmailException -> Exception
Tao 1 doi tuong sinh vien (fullname, age (age > 0), email (@))
-> Tao 1 ham tao day du doi so tren:
	age < 0 -> throw AgeException
	email @ -> EmailException
	age & email & fullname
Main
	Khoi tao doi tuong Student
		- Nhap sai tuoi
		- nhap sai email
		- Khoi tao thanh cong




#AgeException.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11.exception;

/**
 *
 * @author Diep.Tran
 */
public class AgeException extends Exception{
    
}


#EmailException.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11.exception;

/**
 *
 * @author Diep.Tran
 */
public class EmailException extends Exception{
    
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11.exception;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        try {
            Student std = new Student("A", "tranvandiep.it@gmail.com", 1);
            System.out.println("Khoi tao doi tuong thanh cong");
            System.out.println(std);
        } catch (AgeException ex) {
            System.out.println("Yeu cau age >= 0");
        } catch (EmailException ex) {
            System.out.println("Yeu cau email chua ky tu @");
        }
    }
}


#Student.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson11.exception;

/**
 *
 * @author Diep.Tran
 */
public class Student {
    String fullname, email;
    int age;

    public Student(String fullname, String email, int age) throws AgeException, EmailException {
        if(age < 0) {
            throw new AgeException();
        }
        
        if(!email.contains("@")) {
            throw new EmailException();
        }
        
        this.fullname = fullname;
        this.email = email;
        this.age = age;
    }

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


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 đó