Files
Java/Example_src/Chapter1-10/src/ch07/throwsDemo.java
2025-11-06 10:29:13 +08:00

27 lines
786 B
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ch07;
import java.io.IOException;
import java.util.Scanner;
public class throwsDemo {
public static void test() throws NullPointerException, IOException {
System.out.println("请输入一个数字01");
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() + "类型的异常");
}
}
}