Java异常就是Java对程序运行时错误的处理。Java的所有异常都继承自java.lang.Throwable类。Java的异常处理框架如下:
在《EffectIve Java》(高效的Java)一书中关于异常有几点建议:
1、只针对异常的情况才使用异常
2、对可恢复的情况的使用受检异常,对编程错误使用运行时异常
3、避免不必要的使用受检的异常
4、优先使用标准的异常
5、抛出与抽象相对应的异常
6、每个方法抛出的异常都要有文档
//1.非受检异常,无抛出和捕获
public static void main(String[] args) {
testUncheckedException();
System.out.println("next ...");
}
public static void testUncheckedException(){
Integer.parseInt("I am not a number");
System.out.println("then ...");
}
start ...
Exception in thread "main" java.lang.NumberFormatException: For input string: "I am not a number"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at org.littleUtil.ExceptionDemo.testUncheckedException(ExceptionDemo.java:195)
at org.littleUtil.ExceptionDemo.main(ExceptionDemo.java:191)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
//2.非受检异常,捕获异常,也可直接抛出,但会导致方法无法正常运行
public static void main(String[] args){
System.out.println("start ...");
testUncheckedException2();
try {
testUncheckedException3();
}catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println("end ...");
}
public static void testUncheckedException2(){
try {
Integer.parseInt("I am not a number");
}catch (Exception e){
System.out.println(e.getMessage());
}
System.out.println("next ... in 2");
}
public static void testUncheckedException3() throws NumberFormatException{
Integer.parseInt("I am not a number");
System.out.println("next ... in 3");
}
start ...
For input string: "I am not a number"
next ... in 2
For input string: "I am not a number"
end ...
//3.受检的异常
public static void main(String[] args){
System.out.println("start ...");
try {
testCheckedException();
}catch (IOException e){
System.out.println(e.getMessage());
}
System.out.println("end ...");
}
public static void testCheckedException() throws IOException{
File file = new File("D://notfound.txt");
InputStream inputStream = new FileInputStream(file);
inputStream.close();
}
start ...
D:\notfound.txt (系统找不到指定的文件。)
end ...
//4.finally里不应该出现return
public static void main(String[] args) {
System.out.println("start ...");
System.out.println(testFinally1());
System.out.println("end ...");
}
public static String testFinally1(){
try {
if (true){
throw new NumberFormatException("数字转换异常");
}
}catch (NumberFormatException e){
System.out.println(e.getMessage());
throw e;
}finally {
return "OK";
}
}
start ...
数字转换异常
OK
end ...
//5.finally里不应该出现return
public static void main(String[] args) {
System.out.println("start ...");
try {
System.out.println(testFinally2());
}catch (NumberFormatException e){
System.out.println("main:"+e.getMessage());
}
System.out.println("end ...");
}
public static String testFinally2(){
try {
if (true){
throw new NumberFormatException("数字转换异常");
}
}catch (NumberFormatException e){
System.out.println(e.getMessage());
throw e;
}finally {
System.out.println("finally ...");
}
return "OK";
}
start ...
数字转换异常
finally ...
main:数字转换异常
end ...
4和5可以得出结论:如果finally里出现了return则,catch里抛出的异常不会被上一层捕获到。
在java异常机制中finally语句块里的代码始终都会被执行,如果其中出现return则表明程序正常返回,上一层则不会察觉到该段程序的异常问题。