Common exceptions of Java

com.atguigu.java1 Package; 

Import java.io.File;
Import a java.io.FileInputStream;
Import java.util.Date;
Import java.util.Scanner;

Import org.junit.Test;

/ *
* a, the abnormal architecture
*
* java.lang.Throwable
* | ----- java.lang.Error: generally do not write the code targeted for processing.
* | ----- java.lang.Exception: can perform exception handling
* | abnormal (checked) ------ compile time
* | ----- IOException
* | ----- FileNotFoundException
* | ClassNotFoundException -----
* | ------ runtime exceptions (unchecked, RuntimeException)
* | ----- NullPointerException
* | ----- ArrayIndexOutOfBoundsException
* | ----- ClassCastException
* | ----- NumberFormatException
* | ----- InputMismatchException
* | ----- ArithmeticException
*
*
*
* face questions: What are common abnormalities? Illustrates
* /
public class ExceptionTest {

// The following compile-time exception ************************************************** ***************** **********
@Test
public void TEST7 () {
// File = new new File ( "hello.txt") File;
// = the FileInputStream new new FIS the FileInputStream (File);
//
// int Data fis.read = ();
// the while (Data = -1!) {
// of System.out.print ((char) Data);
// Data fis.read = ();
//}
//
// fis.close();

}

//******************以下是运行时异常***************************
//ArithmeticException
@Test
public void test6(){
int a = 10;
int b = 0;
System.out.println(a / b);
}

//InputMismatchException
@Test
public void test5(){
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);

scanner.close();
}

//NumberFormatException
@Test
public void test4(){

String str = "123";
str = "abc";
int num = Integer.parseInt(str);



}

//ClassCastException
@Test
public void test3(){
Object obj = new Date();
String str = (String)obj;
}

//IndexOutOfBoundsException
@Test
public void test2(){
//ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}

//NullPointerException
@Test
public void test1(){

// int[] arr = null;
// System.out.println(arr[3]);

String str = "abc";
str = null;
System.out.println(str.charAt(0));

}


}

Guess you like

Origin www.cnblogs.com/wpy188/p/12088921.html