JAVA finally字句的异常丢失和返回值覆盖解析
JAVA finally字句的异常丢失和返回值覆盖解析
Java虚拟机在每个try语句块和与其相关的catch子句的结尾
处都会“调用”finally子句的子例程。实际上,finally子句在方法内部的表现很象“微型子例程”。finally子句正常结束后-指的是finally子句中最后一条语句正常执行完毕,不包括抛出异常,或执行return、continue、break等情况,隶属于这个finally子句的微型子例程执行“返回”操作。程序在第一次调用微型子例程的地方继续执行后面的语句。
finally“微型子例程”不等同于方法函数的调用,finally子句都是在同一个栈内执行的,微型子例程的“返回”操作也不会涉及到方法退栈,仅仅是使程序计数器pc跳转到同一个方法的一个不同的位置继续执行。
一 异常丢失
public static void exceptionLost()
{
try
{
try
{
throw new Exception( "exception in try" );
}
finally
{
throw new Exception( "exception in finally" );
}
}
catch( Exception e )
{
System.out.println( e );
}
}
exceptionLost()的输出结果是“exception in finally”,而不是try块中抛出的异常,这是JAVA异常机制的一个瑕疵-异常丢失。
在字节码中,throw语句不是原子性操作。在较老的JDK中,exceptionLost()中try块的throw语句分解为几步操作:
1) 把Exception("exception in try")对象引用存储到一个局部变量中
astore_2 // pop the reference to the thrown exception, store into local variable 2
2) 调用finally微型子程序
3) 把局部变量中的Exception("exception in try")对象引用push到操作数栈顶,然后抛出异常
aload_2 // push the reference to the thrown exception from local variable 2
athrow // throw the exception
如果finally通过break、return、continue,或者抛出异常而退出,那么上面的第3步就不会执行。
在JDK1.6中,通过字节码我们可以看到,finally子句作为一种特殊的catch来实现的,下面是exceptionLost()方法的异常表:
Exception table:
from to target type
0 10 10 any
相关文档:
有一道这样的笔试题,对于初学者可能有些难度:
用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845
因为JAVA语言中的long 定义的变量值的最大数受到限制,例如123456789987654321这样的整数就不能存放在long类型的变量中,如果这样两个大数相加或相乘,产生的结果会更大。比如,JAVA语言中如 ......
一、什么是JSON
JSON 即 JavaScript Object Natation(Java对象表示法),它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。
简而言之,JSON就是JavaScript交换数据的一种格式。例子如下:
{"username":"coolcooldool","password":"1230","usertype":"superadmin"}
{"list":[{"password": ......
java中使用scoket模拟http post请求发送图片或文件
最近遇到个问题,两个系统共用用户的头像,用户的头像在一个系统中保存,这就涉及到将图片通过scoket发送给另一个系统存储的问题,最初的思路是将图片读成byte[]数组,然后发送,但又发现,发送图片的同时还要发送密钥、uid、username等信息,好通过对方系统的验证,这就 ......
send mail use smtp .u can send text or html, send to many peoples if u have a email user and pwd and the smtp of the email which u use.
package org.lc.smtp;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mai ......
1. ActionForm中添加属性,FormFile类型对象。
private FormFile upLoadFile;
public FormFile getUpLoadFile() {
return upLoadFile;
}
public void setUpLoadFile(FormFile upLoadFile) {
this.upLoadFile = upLoadFile;
}
2. Action 中增加修改方法。
public ActionForward save(ActionMapping mapping, Action ......