首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

经典的错误

2012-12-21 
经典的异常/**运行时异常*/public class I {public static void main(String []args){ int a10/0 System

经典的异常

/**运行时异常*/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 zeroat 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: 4at 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: 4at User.I.main(I.java:19)



注意:

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

热点排行