finally

object reference set to null in finally block

荒凉一梦 提交于 2019-12-05 00:29:07
public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder=null; /* ;) */ } } why output is CoolReturn, not null? Regards, Mahendra Athneria The expression is evaluated to a value in the return statement, and that's the value which will be returned. The finally block is executed after the expression evaluation part of the return statement. Of course, the finally block could modify the contents of the object referred to by the

Simulating finally block in C++0x

自作多情 提交于 2019-12-04 09:50:59
Inspired from the other topic , I wrote this code which simulates a finally block: #include <cassert> #include <iostream> struct base { virtual ~base(){} }; template<typename TLambda> struct exec : base { TLambda lambda; exec(TLambda l) : lambda(l){} ~exec() { lambda(); } }; class lambda{ base *pbase; public: template<typename TLambda> lambda(TLambda l): pbase(new exec<TLambda>(l)){} ~lambda() { delete pbase; } }; class A{ int a; public: void start(){ int a=1; lambda finally = [&]{a=2; std::cout<<"finally executed";}; try{ assert(a==1); //do stuff } catch(int){ //do stuff } } }; int main() { A

揭开JVM所看到的try/catch/finally

风格不统一 提交于 2019-12-04 08:02:58
#揭开JVM所看到的try/catch/finally 最近有一位朋友发了一段代码给我,这个方法很简单,具体内容大致如下: int num = 5000000;//500万 long begin = System.currentTimeMillis(); for(int i=0; i<num; i++){ try{ //do something }catch(Exception e){ } } long end = System.currentTimeMillis(); System.out.println("==============使用时间:" + (end - begin) + " 毫秒"); 上面代码可以看到是通过执行该循环体所消耗的时间,通过和把 try/cache 注释掉进行对比,最后得到的结果时间比较随机,执行的耗时和 try/cache 没有必然的联系,那 try/cache 究竟会不会影响代码的执行效率呢?从java语言的源码上看貌似多执行了一些指令,实际上是怎么样的呢?下面我分几个场景来分析一下jvm对 try/cache 的处理过程。 ##单层的try/catch 下面是一个只有单层的 try/catch 代码块 public int test(int a,int b){ try{ return a+b; }catch (Exception e){

Are there cases where a “finally” construct would be useful in C++?

妖精的绣舞 提交于 2019-12-04 06:12:00
Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because C++ supports an alternative that is almost always better : The "resource acquisition is initialization" technique (TC++PL3 section 14.4). The basic idea is to represent a resource by a local object, so that the local object's destructor will release the resource. That way, the programmer cannot forget to release the resource. For example: class File_handle { FILE* p; public: File_handle(const char* n, const char* a) { p = fopen(n,a); if (p==0) throw Open_error(errno); } File_handle(FILE* pp) { p = pp; if (p==0

finally in exception handling

巧了我就是萌 提交于 2019-12-04 03:30:14
问题 What exactly does a finally block in exception handling perform? 回答1: It holds code that should always be executed, regardless of whether an exception occurs. For example, if you have opened a file, you should close it in the finally block to ensure that it will always be closed; if you closed it in the try block, an earlier exception would cause execution to jump straight to the catch block and skip closing the file. See the Java tutorials for more details. 回答2: The finally block always

Is there a favored idiom for mimicing Java's try/finally in C++?

偶尔善良 提交于 2019-12-03 12:42:55
问题 Been doing Java for number of years so haven't been tracking C++. Has finally clause been added to C++ exception handling in the language definition? Is there a favored idiom that mimics Java's try/finally? Am also bothered that C++ doesn't have an ultimate super type for all possible exceptions that could be thrown - like Java's Throwable class. I can write: try { // do something } catch(...) { // alas, can't examine the exception // can only do cleanup code and perhaps rethrow, ala: throw;

C# Time of finally execution

扶醉桌前 提交于 2019-12-03 08:35:28
问题 Take this code: using System; namespace OddThrow { class Program { static void Main(string[] args) { try { throw new Exception("Exception!"); } finally { System.Threading.Thread.Sleep(2500); Console.Error.WriteLine("I'm dying!"); System.Threading.Thread.Sleep(2500); } } } } Which gives me this output: Unhandled Exception: System.Exception: Exception! at OddThrow.Program.Main(String[] args) in C:\Documents and Settings\username \My Documents\Visual Studio 2008\Projects\OddThrow\OddThrow

Difference between final keyword, finally block and finalized method in java throught one good example [duplicate]

馋奶兔 提交于 2019-12-03 04:07:51
This question already has answers here : In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? (6 answers) Often these keywords confuse me. Can any one tell me exactly what the difference between them is? final keyword class On a class it means you forbid to have a child class extending yours. public final class finalClass Attribute/Field final MyObject value = new MyObject() means you won't be able to modify the instance of the object. value = xxxx won't be allowed, but you still can modify the object itself value.field = "xxx"; Method When you use final on a method,

Correctly implement finally block using C++ lambda

十年热恋 提交于 2019-12-03 02:42:31
问题 I want to implement a finally block in my C++ program, and the language certainly has the tools to do it if not a native facility. I was wondering what the best way to do this is? 回答1: This simple implementation seems to be 100% safe. template< typename t > class sentry { t o; public: sentry( t in_o ) : o( std::move( in_o ) ) {} sentry( sentry && ) = delete; sentry( sentry const & ) = delete; ~ sentry() noexcept { static_assert( noexcept( o() ), "Please check that the finally block cannot