forked from TechMaster/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowsDemo.java
More file actions
26 lines (20 loc) · 789 Bytes
/
Copy pathThrowsDemo.java
File metadata and controls
26 lines (20 loc) · 789 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
import java.io.IOException;
import java.util.Scanner;
public class ThrowsDemo {
//Tại đây không sử dụng try catch để xử lý mà ta sẽ đẩy trách nhiệm cho phương thức khác
public static int inputAge() throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("Nhập tuổi của bạn: ");
int age = scanner.nextInt();
if(age <= 0) throw new IOException("Tuổi không được nhỏ hơn 0");
return age;
}
public static void getAge() {
try{
int age = inputAge();
System.out.println("Tuổi đã nhập: " + age);
}catch(IOException e){
System.out.println("Tuổi vừa nhập không hợp lệ");
}
}
}