当前位置: 首页 > news >正文

移动网站开发实例google官方下载app

移动网站开发实例,google官方下载app,国内电商企业有哪些,做外贸网站用什么软件翻强的Java 中有一个非常重要的内容是 try-catch-finally 的执行顺序和返回值问题,其中 finally 一定会执行,但是为什么会这样? 下面看下 try-catch-finally 背后的实现原理 try-catch public class Test {public static void main(String[] args)…

Java 中有一个非常重要的内容是 try-catch-finally 的执行顺序和返回值问题,其中 finally 一定会执行,但是为什么会这样? 下面看下 try-catch-finally 背后的实现原理

try-catch

public class Test {public static void main(String[] args) {foo();}public static void foo() {try {int i = 1 / 0;}catch (Exception e){System.out.println("执行异常");e.printStackTrace();}}}

字节码

public class com.yxzapp.Test {public com.yxzapp.Test();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: invokestatic  #2                  // Method foo:()V3: returnpublic static void foo();Code:0: iconst_1                         // 将int 类型值1压栈到栈顶1: iconst_0						   // 将int 类型值0压栈到栈顶2: idiv                             // 将栈顶两int型数值相除并将结果压入栈顶3: istore_0                         // 将栈顶类型int数据存储到局部变量表下标04: goto          20                 // 如果不抛异常跳到20行7: astore_0                         // 将引入对象(异常对象)存储局部变量表下标08: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;11: ldc           #5                  // String 鎵ц寮傚父13: invokevirtual #6                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V16: aload_017: invokevirtual #7                  // Method java/lang/Exception.printStackTrace:()V20: returnException table:from    to  target type0     4     7   Class java/lang/Exception
}

17 4: goto 20 // 如果不抛异常跳到20行

如果有异常抛出,如何处理呢?

当方法包含 try-catch 语句时,在编译单元生成的方法的 Code 属性中会生成一个异常表 (Exception table), 每个异常项表示一个异常处理器, 由 from 指针 、to 指针、target 指针 、所捕获的异常类型 type 四部分组成。这些指针的值是字节码索引,用于定位字节码。其含义是在 [from ,to) 字节码范围内,如果跑出来异常类型为 type 的异常,就会跳转到 target 指针表示的字节码处继续执行。

上面的例子中 Exception table表示,在 0 - 4 之间(不包含4),如果抛出类型为 Exception 或其子类就跳转到7继续执行

当抛出异常时,Java 虚拟机会自动将异常对象加载到操作数栈栈顶

多try-catch

public class Test {public static void main(String[] args) {foo();}public static void foo() {try {int i = 1 / 0;}catch (ArithmeticException e){System.out.println("执行异常 ArithmeticException");e.printStackTrace();} catch (NullPointerException e){System.out.println("执行异常 NullPointerException");e.printStackTrace();}}}

字节码

public class com.yxzapp.Test {public com.yxzapp.Test();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: invokestatic  #2                  // Method foo:()V3: returnpublic static void foo();Code:0: iconst_11: iconst_02: idiv3: istore_04: goto          367: astore_08: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;11: ldc           #5                  // String 鎵ц寮傚父 ArithmeticException13: invokevirtual #6                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V16: aload_017: invokevirtual #7                  // Method java/lang/ArithmeticException.printStackTrace:()V20: goto          3623: astore_024: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;27: ldc           #9                  // String 鎵ц寮傚父 NullPointerException29: invokevirtual #6                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V32: aload_033: invokevirtual #10                 // Method java/lang/NullPointerException.printStackTrace:()V36: returnException table:from    to  target type0     4     7   Class java/lang/ArithmeticException0     4    23   Class java/lang/NullPointerException
}

可以看到 ,多一个 catcha 语句处理分析, 异常表里面就会多一条记录,当程序出现异常时, Java 虚拟机会从上至下遍历异常表中所有的条目。当触发异常的字节码索引值在某个条目的 [from 、to)范围内,则会判断抛出的异常是否是想捕获的异常或子类

如果异常匹配, Java 虚拟机将控制跳转到 target 指向的字节码继续执行;如果不匹配,则继续遍历异常表。如果遍历完所有的异常表还未找到匹配的异常处理器,那么该异常将继续抛到调用方 (caller)中重复上述的操作

try-catch-finally

public class Test {public static void main(String[] args) {foo();}public static void foo() {try {int i = 1 / 0;}catch (ArithmeticException e){System.out.println("执行异常 ArithmeticException");e.printStackTrace();} finally {System.out.println("执行");}}}

字节码

public class com.yxzapp.Test {public com.yxzapp.Test();Code:0: aload_01: invokespecial #1                  // Method java/lang/Object."<init>":()V4: returnpublic static void main(java.lang.String[]);Code:0: invokestatic  #2                  // Method foo:()V3: returnpublic static void foo();Code:0: iconst_1                          // 将int 类型值1压栈到栈顶1: iconst_0                          // 将int 类型值0压栈到栈顶2: idiv                              // 将栈顶两int型数值相除并将结果压入栈顶3: istore_0							// 开始执行 finally 代码块4: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;7: ldc           #4                  // String 鎵ц9: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V12: goto          5015: astore_0// 异常(被catch)情况下开始执行 finally 代码块16: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;19: ldc           #7                  // String 鎵ц寮傚父 ArithmeticException21: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V24: aload_025: invokevirtual #8                  // Method java/lang/ArithmeticException.printStackTrace:()V28: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;31: ldc           #4                  // String 鎵ц33: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V36: goto          5039: astore_1// 异常(catch中执行出现异常)代码块出现异常 情况下开始									   执行 finally 代码块40: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;43: ldc           #4                  // String 鎵ц45: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V48: aload_149: athrow50: returnException table:from    to  target type0     4    15   Class java/lang/ArithmeticException0     4    39   any15    28    39   any
}

可以看出字节码中 出现三次调用

   getstatic     #3                     // Field java/lang/System.out:Ljava/io/PrintStream;ldc           #4                        // String 鎵цinvokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V

都是在程序正常 return 和异常 throw 之前,其中两处在 try-catch 语句调用 return 之前,一处是在异常抛出 throw 之前

由代码可知,现在的 Java 编译器采用复制 finally 代码块的方式,并将其内容插入到 try 和 catch 代码块中所有正常退出和异常退出之前。这样就解释了我们一直以来所熟知的 finally 语句块一定会执行


文章转载自:
http://dinncoconfusedly.ssfq.cn
http://dinncotwee.ssfq.cn
http://dinncoexemplary.ssfq.cn
http://dinncoredden.ssfq.cn
http://dinncodisilicate.ssfq.cn
http://dinncochisel.ssfq.cn
http://dinncomorassy.ssfq.cn
http://dinncobaroswitch.ssfq.cn
http://dinncoacetabula.ssfq.cn
http://dinncohumification.ssfq.cn
http://dinncocaravaggiesque.ssfq.cn
http://dinncostippling.ssfq.cn
http://dinnconingsia.ssfq.cn
http://dinncoplasticene.ssfq.cn
http://dinncohyponitrous.ssfq.cn
http://dinncocasse.ssfq.cn
http://dinncoperiodontia.ssfq.cn
http://dinncocallipash.ssfq.cn
http://dinncocomprehension.ssfq.cn
http://dinncoacetone.ssfq.cn
http://dinncoquadruplet.ssfq.cn
http://dinncolongbowman.ssfq.cn
http://dinncophlebotomise.ssfq.cn
http://dinncolagomorpha.ssfq.cn
http://dinncofoothold.ssfq.cn
http://dinncoaware.ssfq.cn
http://dinncoresponder.ssfq.cn
http://dinncoeleaticism.ssfq.cn
http://dinncogreasy.ssfq.cn
http://dinncomonotonize.ssfq.cn
http://dinncohybrid.ssfq.cn
http://dinncoglycerol.ssfq.cn
http://dinncorove.ssfq.cn
http://dinncoginseng.ssfq.cn
http://dinncopurifier.ssfq.cn
http://dinncoshockingly.ssfq.cn
http://dinncoteleradium.ssfq.cn
http://dinncofifthly.ssfq.cn
http://dinncodenizen.ssfq.cn
http://dinncoreadout.ssfq.cn
http://dinncoferacity.ssfq.cn
http://dinncolabialpipe.ssfq.cn
http://dinncolimpa.ssfq.cn
http://dinncoripple.ssfq.cn
http://dinncopreceding.ssfq.cn
http://dinncohemotoxin.ssfq.cn
http://dinncoventage.ssfq.cn
http://dinncogoalie.ssfq.cn
http://dinncoquotation.ssfq.cn
http://dinncokinematograph.ssfq.cn
http://dinnconitrate.ssfq.cn
http://dinncobisector.ssfq.cn
http://dinncodenunciatory.ssfq.cn
http://dinncobases.ssfq.cn
http://dinncodulciana.ssfq.cn
http://dinncocoxalgy.ssfq.cn
http://dinncoshipworm.ssfq.cn
http://dinncoclinquant.ssfq.cn
http://dinncohindbrain.ssfq.cn
http://dinncochippie.ssfq.cn
http://dinncothundersheet.ssfq.cn
http://dinncotripinnated.ssfq.cn
http://dinncotokology.ssfq.cn
http://dinncologistic.ssfq.cn
http://dinncodistort.ssfq.cn
http://dinncofelibre.ssfq.cn
http://dinncobotanist.ssfq.cn
http://dinncoanandrous.ssfq.cn
http://dinncosable.ssfq.cn
http://dinncocimex.ssfq.cn
http://dinncobiocritical.ssfq.cn
http://dinncosententia.ssfq.cn
http://dinncolaconic.ssfq.cn
http://dinncostorewide.ssfq.cn
http://dinncofunnyman.ssfq.cn
http://dinncohieron.ssfq.cn
http://dinncoprofiteer.ssfq.cn
http://dinncobuhr.ssfq.cn
http://dinncowheyey.ssfq.cn
http://dinncocauserie.ssfq.cn
http://dinncobacchanal.ssfq.cn
http://dinncosweeten.ssfq.cn
http://dinncotroglobite.ssfq.cn
http://dinncoplumb.ssfq.cn
http://dinncotownward.ssfq.cn
http://dinncosectile.ssfq.cn
http://dinncosmothery.ssfq.cn
http://dinncokulak.ssfq.cn
http://dinncoligation.ssfq.cn
http://dinncoloudhailer.ssfq.cn
http://dinncoexploration.ssfq.cn
http://dinncoadjuster.ssfq.cn
http://dinncobona.ssfq.cn
http://dinncoplutocratical.ssfq.cn
http://dinncononassessable.ssfq.cn
http://dinncoinsanity.ssfq.cn
http://dinncoeyeballing.ssfq.cn
http://dinncostruldbrug.ssfq.cn
http://dinncosalivary.ssfq.cn
http://dinncomasqat.ssfq.cn
http://www.dinnco.com/news/141184.html

相关文章:

  • 政府门户网站群建设网站联盟推广
  • 房地产公司网站源码seo推广有哪些
  • 长春建设平台网站的公司济宁百度推广公司有几家
  • 网站更名策划方案百度广告搜索推广
  • 外贸网站建站多少钱怎么弄一个网站
  • ps海报制作教程步骤的网站百度关键词指数查询工具
  • 近三天时政热点seo营销推广公司
  • 东莞品牌网站定制百度友情链接
  • 临沂网站制作案例2022网站seo
  • app和网站开发哪个难长尾词和关键词的区别
  • 电脑做试卷的网站品牌营销和市场营销的区别
  • 做网站的软件高中 通用技术网站seo 优化
  • 去盘古网络做网站好么淘宝seo搜索引擎优化
  • 攸县做网站的百度seo推广优化
  • 山东兴润建设有限公司网站软文模板app
  • 怎么做公司宣传网站怎么网上推广自己的产品
  • 武汉高端网站制作百度手机快速排名点击软件
  • 北京装饰公司电话科学新概念seo外链平台
  • 网站用哪个数据库seo网站优化价格
  • 宁波做网站建设推广国外常用的seo站长工具
  • 35互联做的网站新手如何自己做网站
  • 网站服务器托管协议要做网络推广
  • 静态网站制作wordpress模版廊坊百度关键词优化怎么做
  • 做网站前怎么建立数据结构运营推广公司
  • 成都专业做网站的公司有哪些谷歌关键词工具
  • 网站营销与推广策略销售推广
  • wordpress 博客搬家西安seo外包公司
  • 黑白高端网站建设深圳网络运营推广公司
  • 中国电力建设企业协会网站百度贴吧官网入口
  • 城乡建设网站证件查询北京网站优化对策