finally

finally block does not complete normally

依然范特西╮ 提交于 2019-11-27 12:59:38
原因如下: 不管try块、catch块中是否有return语句,finally块都会执行。 finally块中的return语句会覆盖前面的return语句(try块、catch块中的return语句),所以如果finally块中有return语句,Eclipse编译器会报警告“finally block does not complete normally”。 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。 结论: 应避免在finally块中包含return语句。如果你在前面的语句中包含了return语句或重新抛出了异常,又在finally块中包含了return语句,说明你概念混淆,没有理解finally块的意义。 来源: https://blog.csdn.net/luanxiyuan/article/details/99678234

How to always run some code when a promise is fulfilled in Angular.js

帅比萌擦擦* 提交于 2019-11-27 11:50:26
问题 In my Angular.js application, I'm running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I need to remove the div, whether the operation was successful or not. Currently I have this: LoadingOverlay.start(); Auth.initialize().then(function() { LoadingOverlay.stop(); }, function() { LoadingOverlay.stop(); // Code needs to be duplicated here }) It works well, however I would prefer to have something cleaner like this

Can we use “return” in finally block [duplicate]

北城余情 提交于 2019-11-27 11:20:31
This question already has an answer here: Returning from a finally block in Java 5 answers Multiple returns: Which one sets the final return value? 7 answers Can we use return statement in finally block. Can this cause any problem? Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded. According to the Java Language Specification: If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then

Using python “with” statement with try-except block

和自甴很熟 提交于 2019-11-27 10:46:28
Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: <whatever> If it is, then considering the old way of doing things: try: f = open("file", "r") line = f.readline() except IOError: <whatever> finally: f.close() Is the primary benefit of the "with" statement here that we can get rid of three lines of code? It doesn't seem that compelling to me for this use case (though I understand that the "with" statement has other uses). EDIT: Is the functionality of the above two blocks of

Why doesn't C# have support for first pass exception filtering?

一曲冷凌霜 提交于 2019-11-27 09:51:30
问题 Note: this is not a duplicate of Jeff's question. That question asked "Is an equivalent?" I know there isn't, and I want to know why! The reason I ask is that I've only just become clear on how important it is, and the conclusion seems very strange to me. The Exception Handling block of Microsoft's Enterprise Library advises us to use this pattern: catch (Exception x) { if (ExceptionPolicy.HandleException(x, ExceptionPolicies.MyPolicy)) throw; // recover from x somehow } The policy is defined

Can ThreadAbortException skip finally?

本秂侑毒 提交于 2019-11-27 07:07:58
问题 Everything I've read claims an abort on a thread will execute the finally block before ending from a ThreadAbortException. I wanted to confirm this so I can plan on how to handle some 3rd party code that can hang indefinitely. However the following test has me confused: public void runTest(DateTime deadline) { testThread = new Thread(() => { try { Console.WriteLine("test thread started at " + DateTime.Now.ToShortTimeString()); while (true) { } } finally { Console.WriteLine("test thread

Strange finally behaviour?

自作多情 提交于 2019-11-27 04:50:53
public class Test2 { public static void main(String[] args) { Test2 obj=new Test2(); String a=obj.go(); System.out.print(a); } public String go() { String q="hii"; try { return q; } finally { q="hello"; System.out.println("finally value of q is "+q); } } Why is this printing hii after returning from the function go() , the value has changed to "hello" in the finally block? the output of the program is finally value of q is hello hii That's because you returned a value that was evaluated from q before you changed the value of q in the finally block. You returned q , which evaluated its value;

Why do I need to use finally to close resources?

Deadly 提交于 2019-11-27 04:47:08
问题 Most of the time, the only thing I see a finally block used for is something like FileInputStream f; try{ f= new FileInputStream("sample.txt"); //something that uses f and sometimes throws an exception } catch(IOException ex){ /* Handle it somehow */ } finally{ f.close(); } My question is, if f's scope ends with the enclosing block, why do we need to close it in the finally? 回答1: Because garbage collection is not the same thing as resource cleanup. For example, if you have a JDBC Connection