五种常见的异常

引用
五种常见的异常


package day14.tarena.com;

import java.io.FileInputStream;//读文件

import java.io.FileNotFoundException;

public class TestRuntimeException{

	public static void main(String[] args) {

		int a = 2,b = 0;

		if(b!=0) a = a/b;//算数异常

		String s1 = null;

		if(s1 != null) s1.equals("");//null. 空指针异常

		int[] arr = new int[2];

		int index = 2;

		if(index>=0&&index<arr.length)

		    arr[index] = 2;//数组下标越界异常

		Object obj = 4;

		if(obj instanceof String){

		  String s2 = (String)obj;}//类型转换异常

		String s3 = "abc";

		if(s3.matches("^[-]?\\d+$")){

		int i = Integer.parseInt(s3);

		System.out.println(i);}//数字格式异常

		try {//检测异常,编译时强制处理

			FileInputStream fis = new 

			    FileInputStream("ab.txt");

		}catch(FileNotFoundException e) {

			e.printStackTrace();

		}

	} 

}

你可能感兴趣的:(java)