`

Java中try,return ,finally,throw使用总结

    博客分类:
  • Java
阅读更多
(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() {
		FileInputStream fi = null;
		try {
			fi = new FileInputStream("");
		} catch (FileNotFoundException fnfe) {
			System.out.println("this is FileNotFoundException");
			return 1;
		} catch (SecurityException se) {
			System.out.println("this is SecurityException");
		} finally {
			System.out.println("this is finally");
		}
		return 0;
	}

	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() {
		FileInputStream fi = null;
		try {
			fi = new FileInputStream("");
		} catch (FileNotFoundException fnfe) {
			System.out.println("this is FileNotFoundException");
			return 1;
		} catch (SecurityException se) {
			System.out.println("this is SecurityException");
		} finally {
			System.out.println("this is finally");
			return 3;
		}
		// return 0;
	}

	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 {
	public static void main(String[] args) {
		Test t = new Test();
		try {
			System.out.println(t.testTry());
		} catch (Exception e) {
			System.out.println("this is exception");
		}
	}

	public int testTry() throws Exception {
		FileInputStream fi = null;
		try {
			fi = new FileInputStream("");
		} catch (FileNotFoundException fnfe) {
			// System.out.println("this is FileNotFoundException");
			throw new Exception();
			return 1;
		} catch (SecurityException se) {
			System.out.println("this is SecurityException");
		} finally {
			System.out.println("this is finally");
		}
		return 0;
	}
}
 
>javac Test.java
Test.java:22: 无法访问的语句
Return 1;

情况四:
public class Test {
	public static void main(String[] args) {
		Test t = new Test();
		try {
			System.out.println(t.testTry());
		} catch (Exception e) {
			System.out.println("this is exception");
		}
	}

	public int testTry() throws Exception {
		FileInputStream fi = null;
		try {
			fi = new FileInputStream("");
		} catch (FileNotFoundException fnfe) {
			// System.out.println("this is FileNotFoundException");
			throw new Exception();
			System.out.println("after throw exception");
			// return 1;
		} catch (SecurityException se) {
			System.out.println("this is SecurityException");
		} finally {
			System.out.println("this is finally");
		}
		return 0;
	}
}
 
>javac Test.java
Test.java:22: 无法访问的语句
System.out.println("this is SecurityException");

情况五:
public class Test {
	public static void main(String[] args) {
		Test t = new Test();
		try {
			t.testTry();
		} catch (Exception e) {
			System.out.println("this is exception");
		}
	}

	public void testTry() throws Exception {
		throw new Exception();
		System.out.println("this is testTry method");
	}
}
 
>javac Test.java
Test.java:22: 无法访问的语句
System.out.println("this is testTry method");
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/leigt3/archive/2010/01/15/5193091.aspx
分享到:
评论

相关推荐

    java 面试题 总结

    子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被"屏蔽"了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。...

    java基础课件ppt

    例外处理:throw, try, finally 保留词(无含义但不能使用):goto, const 关键词注意事项 在Java中,true、faslse和null都是小写的。区别于C++中大写的TRUE、FALSE和NULL。 所有的数据类型所占用的字节数都是固定的...

    毕业就业-刷题库Java面试题大全(2021年-2022年).rar

    8. try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗? 9. 类 ExampleA 继承 Exception,类 ExampleB 继承ExampleA。 10. 常见的 RuntimeException 有哪些? 11. Java常见异常有哪些 四、并发...

    java实验2实验报告.doc

    (利用异常处理 编程,尽量使用java现成的异常类,如没有可自定义异常类 ) 三、程序代码 1. public class TestException { public TestException() { } boolean testEx() throws Exception{ boolean ret = true; ...

    JAVA入门1.2.3:一个老鸟的JAVA学习心得 PART1(共3个)

    8.1.2 在Eclipse中使用包 194 8.1.3 天上掉下个package 197 8.1.4 包带来了什么? 197 8.2 import语句:化繁为简 200 8.2.1 import语句 200 8.2.2 一网打尽包中所有类 201 8.2.3 import语句带来的小问题 202 ...

    JAVA中的关键字和保留字

    finally - 7 - float - 8 - for - 8 - if - 8 - implements - 9 - import - 9 - instanceof - 9 - int - 9 - interface - 10 - long - 10 - native - 10 - new - 11 - null - 11 - package - 11 - private - 11 - ...

    Java入门1·2·3:一个老鸟的Java学习心得.PART3(共3个)

    8.1.2 在Eclipse中使用包 194 8.1.3 天上掉下个package 197 8.1.4 包带来了什么? 197 8.2 import语句:化繁为简 200 8.2.1 import语句 200 8.2.2 一网打尽包中所有类 201 8.2.3 import语句带来的小问题 202 ...

    JAVA基础课程讲义

    try, catch,finally ,return 执行顺序 100 异常的处理办法之二,声明异常: throws子句 101 方法重写中声明异常原则 102 异常的处理办法之三,手动抛出异常,throw子句 103 自定义异常 103 使用异常机制建议 104 ...

    JAVA面试题最全集

    50.JAVA语言如何进行异常处理,关键字:thorws,throw,try,catch,finally 51.Object类(或者其子类)的finalize()方法在什么情况下被调用? 52.一个“.java”原文件中是否可以包括多个类(不是内部类)? 53.掌握...

    疯狂JAVA讲义

    10.2.4 使用finally回收资源 364 10.2.5 异常处理的嵌套 367 10.3 Checked异常和Runtime异常体系 367 10.3.1 使用throws声明抛出异常 367 10.4 使用throw抛出异常 369 10.4.1 抛出异常 369 10.4.2 自定义异常...

    Java常见面试题208道.docx

    118.在 hibernate 中使用 Integer 和 int 做映射有什么区别? 119.hibernate 是如何工作的? 120.get()和 load()的区别? 121.说一下 hibernate 的缓存机制? 122.hibernate 对象有哪些状态? 123.在 hibernate 中 ...

    Java问题宝典2012版

    38、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 27 39、下面的程序代码输出的结果是多少? 28 40、final, finally, finalize的区别。 30 ...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    在 java 语言中,Java 程序的基本单位是类,也就是说:一个 Java 程序是由多个类组成 的。定义一个类与定义一个数据类型是有区别的。在程序设计语言中,把定义数据类型的能 力作为一种很重要的能力来对待。在面向...

    史上最全java面试,103项重点知识,带目录

    77. try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗? 36 78. 常见的异常类有哪些? 38 八、网络 39 79. http 响应码 301 和 302 代表的是什么?有什么区别? 39 80. forward 和 redirect 的...

    Java面试宝典2010版

    38、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 25 39、下面的程序代码输出的结果是多少? 25 40、final, finally, finalize的区别。 27 ...

    java万能DAO

    "在实体中无方法名匹配,值将不会被设置!"); } return m; } /** * 删除列分割符 * @return */ private String delLine(String str,String fg){ String result = str; if(str.indexOf(fg)!=-1){ ...

    java面试800题

    finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, ...

    Java面试宝典-经典

    38、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 25 39、下面的程序代码输出的结果是多少? 25 40、final, finally, finalize的区别。 27 ...

    java面试题大全(2012版)

    38、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 25 39、下面的程序代码输出的结果是多少? 25 40、final, finally, finalize的区别。 27 ...

    java面试宝典2012版.pdf

    38、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 39、下面的程序代码输出的结果是多少? 40、final, finally, finalize的区别。 41、...

Global site tag (gtag.js) - Google Analytics