Initial commit
This commit is contained in:
26
Example_src/Chapter1-10/src/ch07/throwsDemo.java
Normal file
26
Example_src/Chapter1-10/src/ch07/throwsDemo.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package ch07;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class throwsDemo {
|
||||
public static void test() throws NullPointerException, IOException {
|
||||
System.out.println("请输入一个数字(0,1):");
|
||||
Scanner in = new Scanner(System.in);
|
||||
int flag = in.nextInt();
|
||||
if (flag == 1)
|
||||
throw new NullPointerException();
|
||||
else
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
test();
|
||||
} catch (NullPointerException e) {
|
||||
System.out.println("系统抛出了" + e.getClass() + "类型的异常");
|
||||
} catch (IOException e) {
|
||||
System.out.println("系统抛出了" + e.getClass() + "类型的异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user