浅谈Java中的错误和异常(Error and Exception)
来源:互联网 发布:淘宝重复铺货开店 编辑:程序博客网 时间:2024/04/28 14:59
面试中经常会被问到error和exception的区别,在这边稍微说一下。下图是一个总的继承关系:
Error: 一般有了Error程序就挂了,而且这时候好像不能人为解决,不能用try/catch解决,比如内存溢出了不够了,这个时候交给JVM接管。
Exception:你写的代码可能会导致的异常,主要分两种,一种是check的编译时异常,一种是runtime的运行时异常。一般可能出现Exception的代码放在try块中,然后在catch块中做相应的处理
CheckedException:顾名思义,就是编译的时候就可以检查出的异常,主要有IOException,InterruptedException。
IOException:比较简单,比如需要读取account.txt文件,结果在给定的路径上jvm没有发现指定的读写文件,就会报 IOException。
InterruptedException:阻塞的线程调用interrupted()方法,该线程会抛出InterruptedException,然后跳出,事例代码如下:
package com.fc.model;public class Test implements Runnable {@Overridepublic void run(){try{Thread.sleep(8000);}catch(InterruptedException ex){System.out.println("Catch an interruptedException");}}public static void main(String[] args){Runnable task=new Test();Thread t= new Thread(task);t.start();t.interrupt();}}最后输出Catch an interruptedException。需要注意的是必须给阻塞中的线程调用interrupt方法才会抛出InterruptedException,然后被catch块接管,所以此处用Thread.sleep(8000)使其进入阻塞态;非阻塞态的线程调用interrupt方法不会有任何变化。
RuntimeException:顾名思义,就是编译的时候就无法检查的异常,只有在运行的时候才有可能发生的异常,主要有ClassCastException,NullPointerException,IndexOutOfBoundsException。
ClassCastException: 向下转型的时候可能会抛出的异常,一个简单的例子就是不加任何泛型的ArrayList,如下:
package com.fc.model;import java.util.ArrayList;public class Test {public static void main(String[] args){ArrayList a=new ArrayList();a.add("1");a.add(2);int i=(int)a.get(0);}}<pre name="code" class="java">Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integerat com.fc.model.Test.main(Test.java:10) 控制台中显示,代码在int i=(int)a.get(0);这句话发生了ClassCastException,因为向不加任何泛型的ArrayList中加入元素再取出来的时候,其类型都是Object,而index为0的位置原来存放的是String类型的"1",取出来是Object,转成int就发生了ClassCastException。
NullPointerException: 字面上翻译为空指针,个人理解就是引用指向了null,试图调用该引用的方法数值什么的就出现了该异常,代码如下:
package com.fc.model;public class Test {static int[] a;public static void main(String[] args){<span style="white-space:pre"></span>System.out.print(a[0]);}}Exception in thread "main" java.lang.NullPointerExceptionat com.fc.model.Test.main(Test.java:10)其中实例静态变量a初始化为null。
IndexOutOfBoundsException: 索引越界了,又分为两个子类:ArrayIndexOutOfBoundsException和StringIndexOutOfBoundsException。这两个从字面上理解就可以了,前一个是数组越界,后一个是字符串越界。代码如下:
package com.fc.model;public class Test {public static void main(String[] args){int[] a={1,2,3};System.out.print(a[6]);}}Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6at com.fc.model.Test.main(Test.java:5)
package com.fc.model;public class Test {public static void main(String[] args){String s="abc";System.out.print(s.charAt(6));}}Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6at java.lang.String.charAt(String.java:658)at com.fc.model.Test.main(Test.java:5)
当然,Exception还远远不止这些,还是那句老话,实践是检验API的唯一真理,多去查API文档才是王道。
- 浅谈Java中的错误和异常(Error and Exception)
- Java基教--异常与错误区别 Error and Exception
- Java--异常与错误区别 Error and Exception
- 异常与错误区别 Error and Exception
- error和exception(错误跟异常)有什么区别
- java中的Error和Exception
- java中的Error和Exception
- Java中的Error和Exception
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- 浅谈java异常[Exception]
- dsad asd
- 网易教育论坛:“状元”在美读大学:中国孩子都被教傻了
- hashmap和hashtable的区别
- Android Screen Monitor (ASM.jar) 抓取Android真机屏幕
- uva10400Game Show Math
- 浅谈Java中的错误和异常(Error and Exception)
- SqlServer_存储过程变量操作简单示例;
- 图库highcharts联合jquery ajax 后端取数据前端图表渲染
- Linux NFS服务器的安装与配置
- 装饰者模式(decorator pattern)
- Unity3d报错解决方案
- 2048私人定制(二)——游戏架构
- Putty VMWare Ubuntu
- TopCoder SRM 634 Div2 Problem 500 - ShoppingSurvey