例1:

class TestExc extends Exception{}
void tryItOut () throws TestExc{}
void handleExc(Object o){}

void catchOne() {
	try {
		tryItOut();
	} catch (TestExc e) {
		handleExc(e);
	}
}

生成的class文件的格式如下:

void catchOne();
    flags:
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: invokevirtual #2                  // Method tryItOut:()V
         4: goto          13
         7: astore_1
         8: aload_0
         9: aload_1
        10: invokevirtual #4                  // Method handleExc:(Ljava/lang/Object;)V
        13: return
      Exception table:
         from    to  target type
            0     4     7   Class com/test19/Test04$TestExc

其中的catchOne()方法的code码如下:

 2A B6 00 02 A7 00 09 4C 2A 2B B6 00 04 B1

opcode [2A] - 0000: aload_0
opcode [B6] - 0001: invokevirtual 2 [Methodref: com.test19.Test04.tryItOut, parameter = (), returns = void]
opcode [A7] - 0004: goto 9
opcode [4C] - 0007: astore_1
opcode [2A] - 0008: aload_0
opcode [2B] - 0009: aload_1
opcode [B6] - 0010: invokevirtual 4 [Methodref: com.test19.Test04.handleExc, parameter = (java.lang.Object), returns = void]
opcode [B1] - 0013: return

例2:

如上的例子与如下的例子生成的class文件一样,但是Exception Table不一样:

class TestExc1 extends Exception{}
class TestExc2 extends Exception{}

void tryItOut () throws TestExc1,TestExc2{}

void handleExc1(Object o){}
void handleExc2(Object o){}

void nestedCatch() {
    try {
        tryItOut();
    } catch (TestExc1 e) {
        handleExc1(e);
    } catch (TestExc2 e) {
        handleExc2(e);
    }
}

生成的class文件的格式如下:

void nestedCatch();
    flags:
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: invokevirtual #2                  // Method tryItOut:()V
         4: goto          22
         7: astore_1
         8: aload_0
         9: aload_1
        10: invokevirtual #4                  // Method handleExc1:(Ljava/lang/Object;)V
        13: goto          22
        16: astore_1
        17: aload_0
        18: aload_1
        19: invokevirtual #6                  // Method handleExc2:(Ljava/lang/Object;)V
        22: return
      Exception table:
         from    to  target type
             0     4     7   Class com/test19/Test07$TestExc1
             0     4    16   Class com/test19/Test07$TestExc2 

再举个例子,如下:

void tryItOut () throws TestExc1,TestExc2{}
class TestExc1 extends Exception{}
class TestExc2 extends Exception{}
void handleExc1(Object o){}
void handleExc2(Object o){}

void nestedCatch() {
	try {
		try {
			tryItOut();
		} catch (TestExc1 e) {
			handleExc1(e);
		}
	} catch (TestExc2 e) {
		handleExc2(e);
	}
}

生成的class文件内容如下:

void nestedCatch();
    flags:
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: invokevirtual #2                  // Method tryItOut:()V
         4: goto          13
         7: astore_1
         8: aload_0
         9: aload_1
        10: invokevirtual #4                  // Method handleExc1:(Ljava/lang/Object;)V
        13: goto          22
        16: astore_1
        17: aload_0
        18: aload_1
        19: invokevirtual #6                  // Method handleExc2:(Ljava/lang/Object;)V
        22: return
      Exception table:
         from    to  target type
             0     4     7   Class com/test19/Test04$TestExc1
             0    13    16   Class com/test19/Test04$TestExc2  

例3:

void tryItOut () {}
void wrapItUp () {}

void tryFinally() {
	try {
		tryItOut();
	} finally {
		wrapItUp();
	}
}

生成的class文件格式如下:

void tryFinally();
    flags:
    Code:
      stack=1, locals=2, args_size=1
         0: aload_0
         1: invokevirtual #2                  // Method tryItOut:()V
         4: aload_0
         5: invokevirtual #3                  // Method wrapItUp:()V
         8: goto          18
        11: astore_1
        12: aload_0
        13: invokevirtual #3                  // Method wrapItUp:()V
        16: aload_1
        17: athrow
        18: return
      Exception table:
         from    to  target type
             0     4    11   any
            11    12    11   any

 

例4:

int tryFinally() {
	try {
		int i = 1;
		return i;
	} finally {
		int j = 2;
		return j;
	}
}

生成的class文件内容如下:

int tryFinally();
    flags:
    Code:
      stack=1, locals=6, args_size=1
         0: iconst_1
         1: istore_1
         2: iload_1
         3: istore_2 // 由于要运行finally,所以需要将这个return的值暂时存储到本地变量表中

         4: iconst_2
         5: istore_3
         6: iload_3
         7: ireturn

         8: astore    4 // 将异常引用类型存储到本地变量表4的位置
        10: iconst_2
        11: istore    5
        13: iload     5 // 将本地变量表5的位置加载到栈中,因为finally中的return值要做为最终值返回
        15: ireturn
      Exception table:
         from    to  target type
             0     4     8   any
             8    10     8   any

例5:

void tryItOut () {}
void wrapItUp1 () {}
void wrapItUp2 () {}

int tryFinally() {
	try {
		try {
			int i = 1;
			return i;
		} finally {
			wrapItUp1();
		}
	} finally {
		wrapItUp2();
	}
}

则生成的代码如下:

stack=1, locals=5, args_size=1
0: iconst_1
1: istore_1
2: iload_1
3: istore_2 // 将变量i的值存储到本地变量表2的位置,以便返回
4: aload_0
5: invokevirtual #2                  // Method wrapItUp1:()V
8: aload_0
9: invokevirtual #3                  // Method wrapItUp2:()V
12: iload_2 // 从本地变量表2的位置取出返回数并返回
13: ireturn

14: astore_3 // 将异常存储到本地变量表3的位置
15: aload_0
16: invokevirtual #2                  // Method wrapItUp1:()V
19: aload_3
// 在这里抛出异常后,在异常表中查看跳转到target,
// 则清空当前操作数栈,异常重新入栈,程序继续执行
20: athrow 

21: astore        4 // 将异常存储到本地变量表4的位置
23: aload_0
24: invokevirtual #3                  // Method wrapItUp2:()V
27: aload         4
29: athrow
Exception table:
from    to  target type
    0     4    14   any
   14    15    14   any
    0     8    21   any
   14    23    21   any

  

例6:

class TestExc1 extends Exception{}
class TestExc2 extends Exception{}

void tryItOut () throws TestExc1,TestExc2{}

void handleExc1(Object o){}
void handleExc2(Object o){}

void wrapItUp () {}

void nestedCatch() {
    try {
        tryItOut();
    } catch (TestExc1 e) {
        handleExc1(e);
    } catch (TestExc2 e) {
        handleExc2(e);
    }finally {
        wrapItUp();
    }
}

  

 

 

  

  

  

关于Gen生成try-catch-finally的更多相关文章

  1. idea 自动生成try/catch代码块的快捷键

    好像每个人的快捷键可能不同:我的是  Alt+Shift+Z 网上查的是  Ctrl+Alt+T 如果都不是可以点选工具栏生成try/catch(并可查看到自己的快捷键是什么):Code->Su ...

  2. C#生成带项目编号的Word段落

    using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; names ...

  3. Eclipse用法和技巧八:自动添加try/catch块1

    站在编译器的角度来看,java中的异常可以分为两种,已检查异常和未检查异常.对于已检查异常比如IO操作,编译器会要求设置try/catch语句块,在eclipse中也只要使用帮助快捷键ctrl+1,就 ...

  4. java生成二维码(最初版)

    研究了2个小时,发现自己竟然智障,用原先的图片覆盖另一个图片 package com.tz.util; import java.awt.Color;import java.awt.Graphics2D ...

  5. 用SAX和PULL进行XML文件的解析与生成

    XML解析有传统的dom方法还有Jsoup,SAX,PULL等,这里讲的是比较省内存的SAX和PULL方法.Android中极力推荐用PULL的方式来解析,我个人觉得pull确实比较简单,但其内部的逻 ...

  6. java图片裁剪和java生成缩略图

    一.缩略图 在浏览相冊的时候.可能须要生成相应的缩略图. 直接上代码: public class ImageUtil { private Logger log = LoggerFactory.getL ...

  7. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  8. Android 如何修改gen下包的名字

    前言 当将项目中包进行重命名后,所有包名字修改了,但是在gen目录下android sdk 自动生成的包名并没有修改,如果要引用R.java 中的字段, 又得import以前的包名字.   原因 出现 ...

  9. JAVA使用qrcode生成二维码(带logo/不带logo)

    /** * */ package qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; i ...

随机推荐

  1. day4之内置函数、匿名函数,递归函数

    内置函数: https://www.processon.com/view/link/5adc6062e4b04721d63171eb?pw=3218 匿名函数:lambda lambda 语法: la ...

  2. spring mvc 注解@Controller @RequestMapping @Resource的详细例子

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  3. PHP Functions - arsort()

    <?php $characters = array('a','b','c','d','e','f'); arsort($characters); print_r($characters); /* ...

  4. [leetcode] 15. Plus One

    这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...

  5. matlab的特殊字符(上下标和希腊字母等)

    'T=25\circC',(摄氏度) 下标用 _(下划线) 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...

  6. 基于CORS的GeoServer跨域访问策略

    GeoServer的跨域访问问题,有多种解决方法,本文介绍一种基于CORS的GeoServer跨域访问方法. CORS简介 CORS是一个W3C标准,全称是"跨域资源共享"(Cro ...

  7. linux系统编程之进程(七):system()函数使用

    一,system()理解 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型: int syst ...

  8. [ASP.NET]NTKO插件使用常见问题

    一.环境要求 NTKO OFFICE文档控件能够在IE.谷歌Chrome.Firefox等浏览器中直接编辑MS Office.WPS.金山电子表.永中Office等文档并保存到WEB服务器.(标准版对 ...

  9. php不用递归完成无限分类,从表设计入手完整演示过程

    无限分类是什么就不废话了,可以用递归实现,但是递归从数据库取东西用递归效率偏低,如果从表设计入手,就很容易做到网站导航的实现,下面是某论坛导航,如下图 网上无限分类大多不全面,今天我会从设计表开始, ...

  10. JEECG(三) JEECG minidao如何封装自己的 多表联合查询 分页查询

    JEECG确实是一款实实在在的促进生产力的工具好处我想看到此文章的人应该都有所体会了 言归正传 JEECG框架自带的查询确实很省事,但是多表联合查询 分页查询 是我们开发业务系统当中不可避免的这时框架 ...