经典的异常

/*
*运行时异常
*/

public class I {

	
	public static void main(String []args)
	{
		 int a=10/0;
		 System.out.println(a);
	}
}

结果:

[color=red]Exception in thread "main" java.lang.ArithmeticException: / by zero
	at User.I.main(I.java:8)[/color]

2.

public class I {

	
	public static void main(String []args)
	{
		
		int a[]={1,2,3};
		System.out.print(a[4]);
	}
}



结果:

[color=red]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at User.I.main(I.java:14)[/color]



把异常处理了


public class I {

	
	public static void main(String []args)
	{
		/*
		 int a=10/0;
		 System.out.println(a);
		 */
		
		int a[]={1,2,3};
		try {
			System.out.print(a[4]);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}

结果:

       空白




无论是否存在异常,到了finally一定执行:

public class I {

	
	public static void main(String []args)
	{
		/*
		 int a=10/0;
		 System.out.println(a);
		 */
		
		int a[]={1,2,3};
		try {
			System.out.print(a[4]);
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			System.out.print(a[4]);
		}
		
	}
}

结果:


[color=red]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at User.I.main(I.java:19)





注意:

  一般情况finally代码块,一般是用来关闭资源的,例如 数据库流  close.conn();

你可能感兴趣的:(java,thread)