finally

Using python “with” statement with try-except block

半世苍凉 提交于 2019-11-26 11:58:08
问题 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

Strange finally behaviour?

拥有回忆 提交于 2019-11-26 11:22:05
问题 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 回答1: That's because you returned a value that was

Does a finally block always run?

半世苍凉 提交于 2019-11-26 10:16:38
Is there any condition where finally might not run in java? Thanks. hhafez from the Sun Tutorials Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. I don't know of any other ways the finally block wouldn't execute... System.exit shuts down the Virtual Machine. Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a

In C# will the Finally block be executed in a try, catch, finally if an unhandled exception is thrown? [duplicate]

流过昼夜 提交于 2019-11-26 09:34:20
问题 Another interview question which was expecting a true / false answer and I wasn\'t too sure. Duplicate In .NET, what if something fails in the catch block, will finally always get called? Does a finally block always run? Conditions when finally does not execute in a .net try..finally block Will code in a Finally statement fire if I return a value in a Try block? 回答1: finally is executed most of the time . It's almost all cases. For instance, if an async exception (like StackOverflowException

Java Try Catch Finally blocks without Catch

拈花ヽ惹草 提交于 2019-11-26 09:16:22
问题 I\'m reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block? 回答1: If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method. The finally block is always executed, whether an

Why do we use finally blocks?

回眸只為那壹抹淺笑 提交于 2019-11-26 08:00:00
问题 As far as I can tell, both of the following code snippets will serve the same purpose. Why have finally blocks at all? Code A: try { /* Some code */ } catch { /* Exception handling code */ } finally { /* Cleanup code */ } Code B: try { /* Some code */ } catch { /* Exception handling code */ } // Cleanup code 回答1: What happens if an exception you're not handling gets thrown? (I hope you're not catching Throwable ...) What happens if you return from inside the try block? What happens if the

Try-catch-finally-return clarification [duplicate]

谁都会走 提交于 2019-11-26 03:49:54
问题 This question already has an answer here: Multiple returns: Which one sets the final return value? 7 answers By reading all the questions already asked in this forum related to the topic above (see title), I thoroughly understand that finally gets always called. (except from System.exit and infinite loops). However, I would like to know if a return is called in a catch block and then another return is called from the finally block. For example: public static void main(String[]args) { int a =

Does C++ support &#39;finally&#39; blocks? (And what&#39;s this &#39;RAII&#39; I keep hearing about?)

China☆狼群 提交于 2019-11-26 01:29:24
问题 Does C++ support \'finally\' blocks? What is the RAII idiom? What is the difference between C++\'s RAII idiom and C#\'s \'using\' statement? 回答1: No, C++ does not support 'finally' blocks. The reason is that C++ instead supports RAII: "Resource Acquisition Is Initialization" -- a poor name † for a really useful concept. The idea is that an object's destructor is responsible for freeing resources. When the object has automatic storage duration, the object's destructor will be called when the