Java中try,return ,finally,throw使用总结:
来源:互联网 发布:windows 欢迎界面图片 编辑:程序博客网 时间:2024/06/08 10:59
Java中try,return ,finally,throw使用总结:
(1) 当try中抛出异常且catch中有return语句,finally中没有return语句,java先执行catch中非return语句,再执行finally语句,最后执行catch中return语句。详见情况一。
(2) 当try中抛出异常且catch中有return语句,finally中也有return语句,java先执行catch中非return语句,再执行finally中非return语句,最后执行finally中return语句,函数返回值为finally中返回的值。详见情况二。
(3) Throw(无能是catch中还是非catch中)后面不能再跟code,否则编译不能通过。详见下面情况三,四,五。
Return,finally总结:
情况一,代码如下:
public class Test {
public int testTry(){
FileInputStreamfi=null;
try{
fi=newFileInputStream("");
}catch(FileNotFoundExceptionfnfe){
System.out.println("this isFileNotFoundException");
return 1;
}catch(SecurityExceptionse){
System.out.println("thisis SecurityException");
}finally{
System.out.println("thisis finally");
}
return0;
}
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.testTry());
}
}
Output:
this is FileNotFoundException
this is finally
1
情况二,代码修改如下:
public class Test {
public int testTry(){
FileInputStreamfi=null;
try{
fi=newFileInputStream("");
}catch(FileNotFoundExceptionfnfe){
System.out.println("this is FileNotFoundException");
return 1;
}catch(SecurityExceptionse){
System.out.println("thisis SecurityException");
}finally{
System.out.println("thisis finally");
return 3;
}
//return0;
}
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.testTry());
}
}
Output:
this is FileNotFoundException
this is finally
3
----------------------------------------------------
Return throw总结:
情况三:
public class Test {
publicstatic void main(String[] args) {
Testt=new Test();
try{
System.out.println(t.testTry());
}catch(Exceptione){
System.out.println("thisis exception");
}
}
publicint testTry()throws Exception{
FileInputStreamfi=null;
try{
fi=newFileInputStream("");
}catch(FileNotFoundExceptionfnfe){
//System.out.println("this is FileNotFoundException");
thrownew Exception();
return 1;
}catch(SecurityExceptionse){
System.out.println("thisis SecurityException");
}finally{
System.out.println("thisis finally");
}
return0;
}
}
>javac Test.java
Test.java:22:无法访问的语句
Return 1;
情况四:
public class Test {
publicstatic void main(String[] args) {
Testt=new Test();
try{
System.out.println(t.testTry());
}catch(Exceptione){
System.out.println("thisis exception");
}
}
publicint testTry()throws Exception{
FileInputStreamfi=null;
try{
fi=newFileInputStream("");
}catch(FileNotFoundExceptionfnfe){
//System.out.println("this isFileNotFoundException");
thrownew Exception();
System.out.println("afterthrow exception");
// return 1;
}catch(SecurityExceptionse){
System.out.println("thisis SecurityException");
}finally{
System.out.println("thisis finally");
}
return0;
}
}
>javac Test.java
Test.java:22:无法访问的语句
System.out.println("this isSecurityException");
情况五:
public class Test {
publicstatic void main(String[] args) {
Testt=new Test();
try{
t.testTry();
}catch(Exceptione){
System.out.println("thisis exception");
}
}
publicvoid testTry()throws Exception{
thrownew Exception();
System.out.println("thisis testTry method");
}
}
>javac Test.java
Test.java:22:无法访问的语句
System.out.println("thisis testTry method");
- Java中try,return ,finally,throw使用总结:
- Java中try,return ,finally,throw使用总结
- Java中try,return ,finally,throw使用总结
- try..finally中使用return
- java中异常的使用try{}catch(){}finally{} ;throw。
- 在try,catch,finally中return,throw覆盖的问题总结
- try-catch-finally中throw,return时的执行顺序
- java try return finally
- java中try,finally和return
- Java中try-catch-finally中的return
- java中try 和 finally中的return
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- SqlDbx的查询返回结果集的问题
- 自定义winform分页控件
- DAV 转 AVI 软件 dav2avi
- JAVA面试题解惑系列(一)——类的初始化顺序
- 远程参数配置工具包
- Java中try,return ,finally,throw使用总结:
- FCKeditor加载完成后做处理的方法
- 创建空画刷
- jboss 连接池配置
- 关于QT Create安装后新建工程编译失败的配置问题
- 计算机高手是怎么炼成的
- 笔记本电脑是时候改变了
- php 调用 sudo 不执行问题
- openSUSE添加启动脚本