27 lines
786 B
Java
27 lines
786 B
Java
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() + "类型的异常");
|
||
}
|
||
}
|
||
}
|