二,JAVA中用2种方法处理异常:
1.在发生异常的地方直接处理;
2.将异常抛给调用者,让调用者处理。
三,JAVA异常可分为3种:
(1)检查性异常:java.lang.Exception
(2)运行期异常:java.lang.RuntimeException
(3)错误:java.lang.Error
顶层是java.lang.Throwable类,检查性异常,运行期异常,错误都是这个类的子孙类。
java.lang.Exception和java.lang.Error继承自java.lang.Throwable,而java.lang.RuntimeException继承自java.lang.Exception.
检查性异常------程序正确,但因为外在的环境条件不满足引发。例如:用户错误及I/O问题----程序试图打开一个并不存在的远程Socket端口。这不是程序本身的逻辑错误,而很可能是远程机器名字错误(用户拼写错误)。对商用软件系统,程序开发者必须考虑并处理这个问题。JAVA编译器强制要求处理这类异常,如果不捕获这类异常,程序将不能被编译。
运行期异常------这意味着程序存在bug,如数组越界,0被除,入参不满足规范.....这类异常需要更改程序来避免,JAVA编译器强制要求处理这类异常。
错误------一般很少见,也很难通过程序解决。它可能源于程序的bug,但一般更可能源于环境问题,如内存耗尽。错误在程序中无须处理,而有运行环境处理
四,try-catch-finally-return执行顺序:
1,try-catch-finally执行顺序:
①,执行try{}块
②,如果try{}块有异常产生,执行catch{}块
③,无论有没有异常都要执行finally{}块,这里可以看出只要finally中有return,必然返回finally{}中的return
2,return的执行,
①,如果finally{}块中有return语句,只执行finally{}块中的return语句
②,如果finally{}块中没有return语句,如果try{}有异常,则返回catch{}中的return语句,不然执行try{}中return语句
在这种情况下,return语句块是在finally{}之后执行
finally{}中有return:
①如果try正常/或异常,则执行finally{}中的return
package com.testjava.Exception;
public class TryCatchfFinally {
public static void main(String[] args){
TryCatchfFinally tcf = new TryCatchfFinally();
System.out.println(tcf.testException());
}
public String testException(){
try {
System.out.println("start try");
int[] a= {2};
for(int i=0 ; i<1;i++){//i=2①
System.out.println(a[i]);
}
System.out.println("try end");
return "return of try not finally";
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("arrayIndexOutOfBoundsException");
return "return in catch arrayIndexOutOfBoundsException";
}catch(NullPointerException e){
System.out.println("nullPointerException");
return "return in catch";
}finally{
System.out.println("finally start try");
int[] a= {2};
for(int i=0 ; i<1;i++){//i=2②
System.out.println(a[i]);
}
System.out.println("finallyend end end");
System.out.println("finally");
return "return of finally";
}
}
}
①处i=1运行结果:
start try
2
try end
finally start try
2
finallyend end end
finally
return of finally
①处i=2运行结果:
start try
2
arrayIndexOutOfBoundsException
finally start try
2
finallyend end end
finally
return of finally
-------------------------------------------------------------------------------------------------
如果finally{}中没有return,
如果try{}正常,执行try{}return语句,如果异常执行catch{}中return语句,但是此事return语句返回在finally{}语句之后
package com.testjava.Exception;
public class TryCatchfFinally {
public static void main(String[] args){
TryCatchfFinally tcf = new TryCatchfFinally();
System.out.println(tcf.testException());
}
public String testException(){
try {
System.out.println("start try");
int[] a= {2};
for(int i=0 ; i<1;i++){//①
System.out.println(a[i]);
}
System.out.println("try end");
return "return of try not finally";
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("arrayIndexOutOfBoundsException");
return "return in catch arrayIndexOutOfBoundsException";
}catch(NullPointerException e){
System.out.println("nullPointerException");
return "return in catch";
}finally{
System.out.println("finally start try");
int[] a= {2};
for(int i=0 ; i<1;i++){//②
System.out.println(a[i]);
}
System.out.println("finallyend end end");
System.out.println("finally");
//return "return of finally";
}
//return "end fuction";
}
}
①处i=1运行结果:
start try
2
try end
finally start try
2
finallyend end end
finally
return of try not finally
①处i=2运行结果:
start try
2
arrayIndexOutOfBoundsException
finally start try
2
finallyend end end
finally
return in catch arrayIndexOutOfBoundsException
-----------------------------------------------------------------------------------------------------
finally{}中有错误,应该是直接退出