[2014-10-22]JAVA笔记_Exception(异常)
2014-10-22 17:54
323 查看
一、 Java中的异常分为两大类
1. Checked exception(非 Runtime exception):
2. Unchecked exception(Runtime exception):
·Java 中所有的异常类都会直接或间接地继承自 Exception。
·RuntimeException类也是直接继承自 Exception类,它叫做运行时异常,Java中所有的运行时异常都会直接或间接地继承自RuntimeException。
·Java中凡是继承自 Exception 而不是继承自 RuntimeException 的类都是非运行时异常。
二、 异常处理的一般结构是:
try{}catch(Exception e){}finally{} 无论程序是否出现异常,finally块中的代码都是会被执行的。
·对于运行时异常(runtime exception),我们可以不对其进行处理,也可以对其进行处理。推荐不对其进行处理。
NullPointerException 是空指针异常,出现该异常的原因在于某个引用为null,但你却调用了它的某个方法。这时就会出现该异常。
三、 自定义异常
所谓自定义异常,通常就是定义了一个继承自Exception类的子类,那么这个类就是一个自定义异常类。通话情况下,我们都会直接继承自 Exception 类,一般不会继承某个运行时的异常类。
·我们可以使用多个 catch 块来捕获异常,这时需要将父类型的 catch 块放到子类型的 catch 块之后,这样才能保证后续的 catch 可能被执行,否则子类型的 catch 将永远无法到达,java 编译器会报编译错误;如果多个catch块的异常类是独立的(MyException, MyException2),那么谁前谁后都是可以的。
package com.bob.exception;
public class ExceptionTest5 {
public void method(){
try{
System.out.println("进入到了try");
// return ; //会先执行finally然后return
System.exit(0); //退出虚拟机。finally不执行
}
catch(Exception e){
System.out.println("异常发生了");
}
finally{
System.out.println("进入到finally块");
}
System.out.println("异常处理后续的代码");
}
public static void main(String[] args) {
ExceptionTest5 test = new ExceptionTest5();
test.method();
}
}
备注:1. 如果 try 块中存在 return 语句,那么首先也需要将 finally 块中的代码执行完毕,然后方法再返回。
2. 如果 try 块中存在 System.exit(0) 语句,那么就不会执行 finally 块中的代码,因为System.exit(0) 会终止当前运行的 Java 虚拟机,程序会在虚拟机终止前结束执行。
1. Checked exception(非 Runtime exception):
2. Unchecked exception(Runtime exception):
·Java 中所有的异常类都会直接或间接地继承自 Exception。
·RuntimeException类也是直接继承自 Exception类,它叫做运行时异常,Java中所有的运行时异常都会直接或间接地继承自RuntimeException。
·Java中凡是继承自 Exception 而不是继承自 RuntimeException 的类都是非运行时异常。
二、 异常处理的一般结构是:
try{}catch(Exception e){}finally{} 无论程序是否出现异常,finally块中的代码都是会被执行的。
package com.bob.exception; public class ExceptionTest2 { public void method() throws Exception{ System.out.println("hello world."); throw new Exception(); //抛出异常 } public static void main(String[] args){ ExceptionTest2 test = new ExceptionTest2(); try{ test.method(); //捕获抛出的异常 }catch(Exception e){ e.printStackTrace(); }finally{ System.out.println("welcome"); } } }·对于非运行时异常(checked exception),必须要对其进行处理,处理方式有两种:第一种是使用try...catch..finally进行捕获;第二种是在调用该会产生异常的方法所在的方法声明 throws Exception(相当于继续往上抛直到main方法,如果main方法也不处理那么就由JVM进行处理)。
·对于运行时异常(runtime exception),我们可以不对其进行处理,也可以对其进行处理。推荐不对其进行处理。
package com.bob.exception; public class ExceptionTest3 { public static void main(String[] args) { String str = null; System.out.println(str.length()); } }上面的程序会报NullPointerException异常。
NullPointerException 是空指针异常,出现该异常的原因在于某个引用为null,但你却调用了它的某个方法。这时就会出现该异常。
三、 自定义异常
所谓自定义异常,通常就是定义了一个继承自Exception类的子类,那么这个类就是一个自定义异常类。通话情况下,我们都会直接继承自 Exception 类,一般不会继承某个运行时的异常类。
package com.bob.exception; public class MyException extends Exception { public MyException(){ super(); } public MyException(String message){ super(message); } }
package com.bob.exception; public class MyExceptionTest { public void method(String str) throws Exception{ if(null == str){ throw new MyException("传入的字符串参数不能为null"); }else{ System.out.println(str); } } public static void main(String[] args) { MyExceptionTest test = new MyExceptionTest(); try{ test.method(null); }catch(Exception e){ e.printStackTrace(); }finally{ System.out.println("异常处理完毕"); } System.out.println("程序执行完毕"); } }
·我们可以使用多个 catch 块来捕获异常,这时需要将父类型的 catch 块放到子类型的 catch 块之后,这样才能保证后续的 catch 可能被执行,否则子类型的 catch 将永远无法到达,java 编译器会报编译错误;如果多个catch块的异常类是独立的(MyException, MyException2),那么谁前谁后都是可以的。
package com.bob.exception;
public class ExceptionTest5 {
public void method(){
try{
System.out.println("进入到了try");
// return ; //会先执行finally然后return
System.exit(0); //退出虚拟机。finally不执行
}
catch(Exception e){
System.out.println("异常发生了");
}
finally{
System.out.println("进入到finally块");
}
System.out.println("异常处理后续的代码");
}
public static void main(String[] args) {
ExceptionTest5 test = new ExceptionTest5();
test.method();
}
}
备注:1. 如果 try 块中存在 return 语句,那么首先也需要将 finally 块中的代码执行完毕,然后方法再返回。
2. 如果 try 块中存在 System.exit(0) 语句,那么就不会执行 finally 块中的代码,因为System.exit(0) 会终止当前运行的 Java 虚拟机,程序会在虚拟机终止前结束执行。
相关文章推荐
- Java基础笔记 – Java中的Exception异常 自定义异常
- Java异常(Exception)小例子&知识点【Java学习笔记】
- Java 复习笔记 异常Exception
- 学习笔记_java.lang.reflect.Field_解决IllegalAccessException异常的方法
- Java学习笔记26 异常Exception
- [笔记]关于split的数组越界问题以及java.lang.ArrayIndexOutOfBoundsException异常
- 自定义异常类学习笔记 ExceptionDemo3.java
- Thinking in Java学习笔记 Thread.UncaughtExceptionHandler接口实现捕获线程内异常
- Think in Java 笔记_Chapter12_1_Exception基础_异常处理3_自定义异常以及抛出多个异常的处理
- 【马士兵】笔记_Java异常Exception
- Java基础笔记 – Java中的Exception异常 自定义异常
- Java 学习笔记 (4) -异常 Exception
- Java笔记2 面向对象<5>内部类、静态内部类、内部类定义原则、匿名内部类、异常概述、RuntimeException
- JAVA学习笔记25——异常机制1:Exception简介+异常处理的try_catch_finally方法
- 异常抛出学习笔记ExceptionDemo2.java
- Java学习笔记61. 程序抛出的异常Exception
- Tomcat:IOException while loading persisted sessions: java.io.EOFException异常处理
- [Domino]“java.lang.ClassCastException:lotus.domino.cso.Item”异常解决办法
- EJB java.io.NotSerializableException 异常解决方法
- java.util.ConcurrentModificationException 异常