try-finally

Python try finally block returns [duplicate]

旧巷老猫 提交于 2019-12-17 15:37:24
问题 This question already has answers here : Weird Try-Except-Else-Finally behavior with Return statements (2 answers) Closed 6 years ago . There is the interesting code below: def func1(): try: return 1 finally: return 2 def func2(): try: raise ValueError() except: return 1 finally: return 3 func1() func2() Could please somebody explain, what results will return these two functions and explain why, i.e. describe the order of the execution 回答1: From the Python documentation A finally clause is

finally not called after try

眉间皱痕 提交于 2019-12-14 01:25:13
问题 For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple: static void Main() { int i = 0; try { int j = 1 / i; // Generate a divide by 0 exception. } finally { Console.Out.WriteLine("Finished"); Console.In.ReadLine(); } } At first I had the problem described here but then I tried to run the program outside Visual Studio I got a "Program has stopped responding" error. 回答1: Wanted to add

Use of nested “try/finally” “try/except” statements

纵然是瞬间 提交于 2019-12-13 00:33:27
问题 I have seen this code posted here on StackOverflow: with TDownloadURL.Create(nil) do try URL := 'myurltodownload.com'; filename := 'locationtosaveto'; try ExecuteTarget(nil); except result := false; end; if not FileExists(filename) then result := false; finally free; end; Can't it be simplified to look like: Result:= FALSE; <--------- Compiler complains DeleteFile(Dest); dl:= TDownloadURL.Create(NIL); TRY dl.URL:= URL; dl.FileName:= Dest; dl.ExecuteTarget(NIL); Result:= FileExists(Dest);

Extract nested try/finally blocks

怎甘沉沦 提交于 2019-12-11 15:32:56
问题 How would you "extract" nested try/finally blocks from a routine into a reusable entity? Say I have procedure DoSomething; var Resource1: TSomeKindOfHandleOrReference1; Resource2: TSomeKindOfHandleOrReference2; Resource3: TSomeKindOfHandleOrReference3; begin AcquireResource1; try AcquireResource2; try AcquireResource3; try // Use the resources finally ReleaseResource3; end; finally ReleaseResource2; end; finally ReleaseResource1; end; end; and want something like TDoSomething = record // or

When Does the Finally Block Run Relative to the Return

折月煮酒 提交于 2019-12-11 06:24:55
问题 I stumbled across an interesting error yesterday and have since fixed it, but it still was bothering me this morning, so I would like to see if anyone can shed some light on the issue. The code in question: final ResultSet rs = prepStatement.executeQuery(); try { if (!rs.next()) { throw new IllegalStateException("Expected non-empty result"); } return rs.getInt(0 + 1); } finally { rs.close(); } Now for the part that doesn't make since. Every once in a while, the return statement will throw an

What's the scope of using the 'finally' clause in python? [duplicate]

给你一囗甜甜゛ 提交于 2019-12-11 03:06:22
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Purpose of else and finally in exception handling I'd like to understand why the finally clause exists in the try/except statement. I understand what it does, but clearly I'm missing something if it deserves a place in the language. Concretely, what's the difference between writing a clause in the finally field with respect of writing it outside the try/except statement? 回答1: The finally suite is guaranteed to

Double exception throwing in a try / finally block

匆匆过客 提交于 2019-12-10 16:26:23
问题 Here's the code example : Try Throw New FirstException() Finally Throw New SecondException() End Try I figured out it only throws SecondException out and FirstException just vanishes. I thought FirstException would be inside InnerException property of SecondException but it appears it is not. I'm not blocked on anything as I don't really need the FirstException to show up, I'm just rather intrigued about this behaviour. Is there a way to know SecondException did get thrown first when catching

Using try-finally block inside while loop [duplicate]

ぐ巨炮叔叔 提交于 2019-12-10 13:47:58
问题 This question already has answers here : Does a finally block always get executed in Java? (47 answers) Closed last year . I am trying to understand the mechanism when i use finally inside a while loop. In the below code. In finally line prints and than the while breaks. I was expecting the code not to reach the finally block. Or if it reaches the finally block, there is no break there so the while should continue.. Can anyone explain how this works ? while(true){ System.out.println("in while

Using finally instead of catch

南笙酒味 提交于 2019-12-08 16:37:22
问题 I've seen this pattern a few times now: bool success = false; try { DoSomething(); success = true; } finally { if (!success) Rollback(); } And I've been wondering: Why is this better than using catch for rollbacks? try { DoSomething(); } catch { Rollback(); throw; } What are the differences between the two ways of making sure changes are rolled back on failure? 回答1: The clear goal here is to cause Rollback to be called in the event of any error. Both code snippets accomplish this goal. The

Workaround for python 2.4's yield not allowed in try block with finally clause

陌路散爱 提交于 2019-12-07 07:58:44
问题 I'm stuck on python2.4, so I can't use a finally clause with generators or yield . Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable time) aren't very appealing. 回答1: You can duplicate code to avoid the finally block: try: yield 42 finally: do_something() Becomes: try: yield 42 except: # bare