try-finally

How to determine if an exception was raised once you're in the finally block?

孤街醉人 提交于 2019-11-29 00:20:20
问题 Is it possible to tell if there was an exception once you're in the finally clause? Something like: try: funky code finally: if ???: print('the funky code raised') I'm looking to make something like this more DRY: try: funky code except HandleThis: # handle it raised = True except DontHandleThis: raised = True raise else: raised = False finally: logger.info('funky code raised %s', raised) I don't like that it requires to catch an exception, which you don't intend to handle, just to set a flag

What happens if both catch and finally blocks throw exception?

百般思念 提交于 2019-11-28 18:36:36
What happens if both catch and finally blocks throw exception? When the finally block throws an exception, it will effectively hide the exception thrown from the catch block and will be the one ultimately thrown. It is therefore important to either log exceptions when caught, or make sure that the finally block does not itself throw an exception, otherwise you can get exceptions being thrown that are stifled and never seen. When catch throws an exception, finally block will be run and then exit with an exception. If the finally block throws an exception, the block will exit with an exception.

Why does a return in `finally` override `try`?

旧城冷巷雨未停 提交于 2019-11-28 16:09:20
How does a return statement inside a try/catch block work? function example() { try { return true; } finally { return false; } } I'm expecting the output of this function to be true , but instead it is false ! Finally always executes. That's what it's for, which means it's return gets used in your case. You'll want to change your code so it's more like this: function example() { var returnState = false; // initialisation value is really up to the design try { returnState = true; } catch { returnState = false; } finally { return returnState; } } Generally speaking you never want to have more

Difference between try-finally and try-catch

限于喜欢 提交于 2019-11-28 15:35:44
问题 What's the difference between try { fooBar(); } finally { barFoo(); } and try { fooBar(); } catch(Throwable throwable) { barFoo(throwable); // Does something with throwable, logs it, or handles it. } I like the second version better because it gives me access to the Throwable. Is there any logical difference or a preferred convention between the two variations? Also, is there a way to access the exception from the finally clause? 回答1: These are two different things: The catch block is only

Try-finally block prevents StackOverflowError

感情迁移 提交于 2019-11-28 14:57:06
Take a look at the following two methods: public static void foo() { try { foo(); } finally { foo(); } } public static void bar() { bar(); } Running bar() clearly results in a StackOverflowError , but running foo() does not (the program just seems to run indefinitely). Why is that? It doesn't run forever. Each stack overflow causes the code to move to the finally block. The problem is that it will take a really, really long time. The order of time is O(2^N) where N is the maximum stack depth. Imagine the maximum depth is 5 foo() calls foo() calls foo() calls foo() calls foo() which fails to

Overhead of try/finally in C#?

点点圈 提交于 2019-11-28 04:36:52
We've seen plenty of questions about when and why to use try / catch and try / catch / finally . And I know there's definitely a use case for try / finally (especially since it is the way the using statement is implemented). We've also seen questions about the overhead of try/catch and exceptions . The question I linked to, however, doesn't talk about the overhead of having JUST try-finally. Assuming there are no exceptions from anything that happens in the try block, what's the overhead of making sure that the finally statements get executed on leaving the try block (sometimes by returning

FURTHER CLARIFICATION: How to correctly write Try..Finally..Except statements?

天大地大妈咪最大 提交于 2019-11-28 02:15:08
问题 RE: How to correctly write Try..Finally..Except statements? I'm still confused by the OP's original question. Specifically, the last line of the procedure (outside of the try..finally..end) that reads "Screen.Cursor:=crDefault". My understanding is that any exceptions raised inside a try..except|finally..end block WILL execute the code after the "end" of the "try". procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor := crHourGlass; Obj := TSomeObject

Stack overflow error handling in finally block

此生再无相见时 提交于 2019-11-28 01:27:08
问题 I have a program in java, which runs infinite times. Program code: void asd() { try { //inside try block System.out.println("Inside try !!!"); asd(); } finally { //inside finally System.out.println("Inside finally !!!"); asd(); } } OUTPUT : this program runs infinitely, by constantly printing both the sysouts. My question : At some point, it starts throwing StackOverflowErrors from the try block and so it reaches the finally block, where we are again calling this function recursively. But how

Try-finally block prevents StackOverflowError

岁酱吖の 提交于 2019-11-27 19:44:50
问题 Take a look at the following two methods: public static void foo() { try { foo(); } finally { foo(); } } public static void bar() { bar(); } Running bar() clearly results in a StackOverflowError , but running foo() does not (the program just seems to run indefinitely). Why is that? 回答1: It doesn't run forever. Each stack overflow causes the code to move to the finally block. The problem is that it will take a really, really long time. The order of time is O(2^N) where N is the maximum stack

Python try finally block returns [duplicate]

心不动则不痛 提交于 2019-11-27 18:56:34
This question already has an answer here: Weird Try-Except-Else-Finally behavior with Return statements 2 answers 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 lejlot From the Python documentation A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in