本文章主要对比 java和C#执行finally的异同。
一:C#中的处理
C#代码
class Program
{
static void error()
{
throw new Exception();
}
static int test()
{
int a = 0;
try
{
a++;
return a;
}
catch (Exception)
{
a++;
return a;
}
finally
{
a++;
//return a; 在C#中编译器做了限制,提示控制不能离开 finally 子句主体,可以意外的修改了返回值
}
}
static void Main(string[] args)
{
test();
Console.ReadKey();
}
}
应的汇编代码
{ 00000000 push ebp 00000001 mov ebp,esp 00000003 push edi 00000004 push esi 00000005 push ebx 00000006 sub esp,38h 00000009 lea edi,[ebp-44h] 0000000c mov ecx,0Eh 00000011 xor eax,eax 00000013 rep stos dword ptr es:[edi] 00000015 cmp dword ptr ds:[05A81010h],0 0000001c je 00000023 0000001e call 61AD423A 00000023 xor edx,edx 00000025 mov dword ptr [ebp-3Ch],edx 00000028 xor edx,edx 0000002a mov dword ptr [ebp-40h],edx 0000002d nop int a = 0; 0000002e xor edx,edx 00000030 mov dword ptr [ebp-3Ch],edx try { 00000033 nop a++; 00000034 inc dword ptr [ebp-3Ch] return a; 00000037 mov eax,dword ptr [ebp-3Ch] 0000003a mov dword ptr [ebp-40h],eax 0000003d nop 0000003e mov dword ptr [ebp-20h],0 00000045 mov dword ptr [ebp-1Ch],0FCh 0000004c push 1552F1Fh 00000051 jmp 0000007A //跳转到finally } catch (Exception) 00000053 mov dword ptr [ebp-44h],eax { 00000056 nop a++; 00000057 inc dword ptr [ebp-3Ch] return a; 0000005a mov eax,dword ptr [ebp-3Ch] 0000005d mov dword ptr [ebp-40h],eax 00000060 call 6183F8CD 00000065 mov dword ptr [ebp-20h],0 0000006c mov dword ptr [ebp-1Ch],0FCh 00000073 push 1552F16h 00000078 jmp 0000007A //跳转到finally } finally { 0000007a nop a++; 0000007b inc dword ptr [ebp-3Ch] } 0000007e nop 0000007f pop eax 00000080 jmp eax 00000082 nop } 00000083 mov eax,dword ptr [ebp-40h] 00000086 lea esp,[ebp-0Ch] 00000089 pop ebx 0000008a pop esi 0000008b pop edi 0000008c pop ebp 0000008d ret 0000008e mov dword ptr [ebp-1Ch],0 00000095 jmp 00000082 00000097 mov dword ptr [ebp-1Ch],0 0000009e jmp 00000082
从上面汇编代码中,可以看到 finally 时在return 之后执行。如果我们在finally中添加返回值,系统提示:
不能离开 finally 子句主体
在C#中编译器做了限制,可以防止我们意外的修改了返回值。
二:java中的处理
java代码
public class mainn { static int test() { int a=0; try { a++; int c = Integer.parseInt("33d"); return a; } catch(Exception ex) { a++; return a; } finally { a++; //return a; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int c = test(); System.out.println(c); } }
在没有异常的情况下输出1,有异常的情况下输出2。如果我们在finally中增加返回值,可以看到返回结果是3。感觉这点java没有C#做的好。
##########################2014-05-27####################
今天看到这篇文章,放进来
finally子语里的return...险恶啊 http://rednaxelafx.iteye.com/blog/189908
相关推荐
简单性 Java看起来设计得很像C++,但是为了使语言小和容易熟悉,设计者们把C++语言中许多可用的特征去掉了,这些特征是...使用try/catch/finally语句,程序员可以找到出错的处理代码,这就简化了出错处理和恢复的任务。
看完了前面几段,我的朋友提出了不同的意见:C#不是Java的Clone,它只是长得有些像Java而已,其实面向对象、中间语言什么的也不是什么新玩意儿,非Sun独创,有文为证:华山论剑:C#对Java。另外他对我上一集中说...
1.2.1 C#简介和优势 4 1.2.2 Ruby简介和优势 4 1.2.3 Python的简介和优势 5 1.3 Java程序运行机制 5 1.3.1 高级语言的运行机制 6 1.3.2 Java程序的运行机制和JVM 6 1.4 开发Java的准备 7 1.4.1 安装JDK 8 ...
无论是刚开始接触面向对象编程的新手,还是打算转移到c#的具有c,c++或者java基础的程序员,都可以从本书中吸取到新的知识。 作译者 john sharp,content master首席技术专家。content master隶属于cm集团,cm集团...
c#中的委托,事件是不是委托? 答:委托就是将方法作为一个参数带入另一个方法叫做委托,事件是一种特殊的委托。 应用程序域? 答:应用程序域可以理解为一种轻量级的进程,起到安全的作用,占用资源小。 Class.for...
In fact, the power of C# and the easy-to-learn syntax persuaded many developers to switch to it from C++ or Java. The C# 2005 version (also referred to as C# 2.0) added even more powerful features to...
积分java源码stab 语言 自动导出自 一种为 Java 虚拟机设计的多范式编程语言 设计目标 用于设计 Stab 的目标如下: ...finally和throw处理异常 synchronized块和方法 编译时通用编程,包括约束和通配符 注释
•对于自动关闭资源的try语句, 可以没有catch和finally——try块可以孤独地存在。 •自动关闭资源的try语句,有两个注意点: –只有放在try后面的圆括号里的资源才会被关闭。 –能被自动关闭的资源必须实现...
and finally, delivering functionality through ASP.NET and Web Services. <br> .NET Framework and Common Language Runtime fundamentals for experienced programmers <br>Key .NET features: ...
I’m assuming that you’re familiar with a programming language such as C++, C#, Visual Basic, or Java. I also assume that you’re familiar with object-oriented programming concepts such as ...
C#任务的轻量级扩展:添加Then(...) , Catch(...)和Finally(...)方法,以轻松地将任务链接在一起并处理错误。 它提供了与async / await非常相似的语法。 因此,它是.Net <4.5项目的理想解决方案,这些项目...
Using standard programming tools and the intuitive Oculus SDKs, you can deliver powerful immersive games, simulations, and other virtual experiences that finally nail the feeling of being in the ...
Harry has extensive experience in C#/.NET, Java and Scala, and JavaScript/Node.js. He continues to work directly with these technologies on a regular basis in the teams that he leads. His broader ...
Finally, you will get an understanding of how to integrate HBase with other tools such as ElasticSearch. By the end of this book, you will have learned enough to exploit HBase for boost system ...
GoF 的《设计模式》是所有面向对象语言(C++ Java C#)的基础,只不过不同的语言将之实现得更方便地使用。 GOF 的设计模式是一座"桥" 就 Java 语言体系来说,GOF 的设计模式是 Java 基础知识和 J2EE 框架知识之间一...
Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex ...
This book is intended for software quality assurance/testing professionals, software project managers, or software developers with prior experience in using Selenium and Java to test web-based ...
43.try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 答:会执行,在return前执行。 44.两个对象值相同(x.equals(y) == true),但却可有不同...
一文让你彻底理解 Java HashMap 和 ConcurrentHashMap 2018-07-25 分类:JAVA开发、编程开发、首页精华0人评论 来源:crossoverjie.top 分享到:更多0 前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常...