Java 异常处理——1

package exception;

public class TestException {
	
	public static void main(String[] args) {
	try{
		for(int i=0;i<4;i++)
		{
			int  k;
			switch(i)
			{
				case 0:
		            int zero=0;
		            k=911/zero;
		            break;
				case 1:
		            int  b[]=null;
		            k = b[0];
		            break;
				case 2:
		            int c[]=new int[2];
		            k=c[9];
		            break;
				case  3:
		           char  ch="abc".charAt(99);
		           break;
			}
		}
	}
	
	
	catch (ArithmeticException ex)
	{
		ex.printStackTrace();
		System.out.println("算数运算错误");
	}catch (NullPointerException ex)
	{
		ex.printStackTrace();
		System.out.println("空指针异常");
	}
	catch(ArrayIndexOutOfBoundsException ex)
	{
		System.out.println("数组下标越界错误");
	}

	catch (NumberFormatException ex)
	{
		System.out.println("数字转换成字符串异常");
	}
	catch (Exception ex)
	{
		System.out.println("其他异常");
	}
	
 } 
}

posted @ 2016-05-28 16:55  jakermk  阅读(127)  评论(0编辑  收藏  举报