-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathThrowDemo.java
More file actions
27 lines (23 loc) · 872 Bytes
/
Copy pathThrowDemo.java
File metadata and controls
27 lines (23 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.InputMismatchException;
import java.util.Scanner;
public class ThrowDemo {
// Ví dụ: Kiểm tra tuổi, nếu tuổi nhỏ hơn 18, ném ra ngoại lệ thông báo chưa đủ
// tuồi bầu cử
public static void validate() {
try {
int age = inputAge();
if (age < 18)
throw new ArithmeticException("Bạn chưa đủ tuổi để tham gia bầu cử");
else
System.out.println("Bạn đã đủ tuổi bầu cử");
} catch (InputMismatchException e) {
System.out.println("Tuổi nhập vào chưa hợp lệ");
}
}
public static int inputAge(){
Scanner scanner = new Scanner(System.in);
System.out.println("Nhập tuổi của bạn: ");
int age = scanner.nextInt();
return age;
}
}