JAVA---编译时异常举例

package exer;


import java.util.Date;
import java.util.Scanner;

import org.junit.Test;

public class ExceptionTest {
	//运行时异常
	
	//NullPointerException
	@Test
	public void test1(){
		String str="abc";
		str=null;
		System.out.println(str.charAt(0));
	}
	//IndexOutOfBoundsException
	@Test
	public void test2(){
		//ArrayIndexOutOfBoundsException
//		int[] arr=new int[10];
//		System.out.println(arr[10]);
		//StringIndexOutOfBoundException
		String str="abc";
		System.out.println(str.charAt(3));
	}
	
	//ClassCastException
	@Test
	public void test3(){
		Object obj=new Date();
		String str=(String)obj;
	}
	
	//NumberFormatException
	@Test
	public void test4(){
		String str="abc";
		int num=Integer.parseInt(str);
	}
	//InputMismatchException
	@Test
	public void test5(){
		Scanner scanner=new Scanner(System.in);
		int score=scanner.nextInt();
		System.out.println(score);
	}
	
	//ArithmeticException
	@Test
	public void test6(){
		int a=10;
		int b=0;
		System.out.println(a/b);
	}

}

posted @ 2022-02-21 17:33  ice--cream  阅读(110)  评论(0编辑  收藏  举报