Java-异常
1.概念
2.异常体系结构
* java.lang.Throwable
* |-----java.lang.Error:一般不编写针对性的代码进行处理。
* |-----java.lang.Exception:可以进行异常的处理
* |------编译时异常(checked)
* |-----IOException
* |-----FileNotFoundException
* |-----ClassNotFoundException
* |------运行时异常(unchecked,RuntimeException)
* |-----NullPointerException
* |-----ArrayIndexOutOfBoundsException
* |-----ClassCastException
* |-----NumberFormatException
* |-----InputMismatchException
* |-----ArithmeticException
3.code举例
package p8exception;
public class ErrorTest {
public static void main(String[] args) {
main(args);
Integer[] arr = new Integer[1024*1024*1024];
}
}
package p8exception;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
public class ExceptionTest {
@Test
public void test7() {
File file = new File("hello.txt");
FileInputStream fils = new FileInputStream(file);
int data = fils.read();
while (data != -1) {
System.out.print(data);
}
fils.close();
}
@Test
public void test1() {
int[] arr = null;
System.out.println(arr[3]);
String str = null;
System.out.println(str.toString());
}
@Test
public void test2() {
String str = "aka";
System.out.println(str.charAt(3));
}
@Test
public void test3() {
Object obj = new Date();
String str = (String)obj;
}
@Test
public void test4() {
String str = "abc";
int num = Integer.parseInt(str);
}
@Test
public void test5() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字:");
int score = scanner.nextInt();
System.out.println(score);
}
@Test
public void test6() {
int a = 10;
int b = 0;
System.out.println(a/b);
}
}
n
@Test
public void test6() {
int a = 10;
int b = 0;
System.out.println(a/b);
}
}