try-except

How to correctly write Try..Finally..Except statements?

青春壹個敷衍的年華 提交于 2019-11-29 05:13:34
问题 Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor:= crHourGlass; Obj:= TSomeObject.Create; try // do something finally Obj.Free; end; Screen.Cursor:= crDefault; end; if there was an error happening in the // do something section, the TSomeObject that was created I assume will not be freed and the Screen.Cursor will still be stuck as an Hour Glass, because the code was broke before getting to those lines? Now unless I

How to make a variable inside a try/except block public?

不想你离开。 提交于 2019-11-28 10:41:57
How can I make a variable inside the try/except block public? import urllib.request try: url = "http://www.google.com" page = urllib.request.urlopen(url) text = page.read().decode('utf8') except (ValueError, RuntimeError, TypeError, NameError): print("Unable to process your request dude!!") print(text) This code returns an error NameError: name 'text' is not defined . How can I make the variable text available outside of the try/except block? try statements do not create a new scope, but text won't be set if the call to url lib.request.urlopen raises the exception. You probably want the print

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

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

How to make a variable inside a try/except block public?

吃可爱长大的小学妹 提交于 2019-11-27 03:02:23
问题 How can I make a variable inside the try/except block public? import urllib.request try: url = "http://www.google.com" page = urllib.request.urlopen(url) text = page.read().decode('utf8') except (ValueError, RuntimeError, TypeError, NameError): print("Unable to process your request dude!!") print(text) This code returns an error NameError: name 'text' is not defined How can I make the variable text available outside of the try/except block? 回答1: try statements do not create a new scope, but

How to retry after exception?

痞子三分冷 提交于 2019-11-26 14:48:46
I have a loop starting with for i in range(0, 100) . Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will continue in the except clause (continue on to the next number for i ). Is it possible for me to reassign the same number to i and run through the failed iteration of the loop again? Do a while True inside your for loop, put your try code inside, and break from that while loop only when your code succeeds. for i in range(0,100): while True: try: # do stuff except SomeSpecificException: continue break I prefer to

Weird Try-Except-Else-Finally behavior with Return statements

核能气质少年 提交于 2019-11-26 14:27:49
This is some code that is behaving peculiarly. This is a simplified version of the behavior that I've written. This will still demonstrate the weird behavior and I had some specific questions on why this is occurring. I'm using Python 2.6.6 on Windows 7. def demo1(): try: raise RuntimeError,"To Force Issue" except: return 1 else: return 2 finally: return 3 def demo2(): try: try: raise RuntimeError,"To Force Issue" except: return 1 else: return 2 finally: return 3 except: print 4 else: print 5 finally: print 6 Results: >>> print demo1() 3 >>> print demo2() 6 3 Why is demo one returning 3

How to retry after exception?

烂漫一生 提交于 2019-11-26 03:46:28
问题 I have a loop starting with for i in range(0, 100) . Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will continue in the except clause (continue on to the next number for i ). Is it possible for me to reassign the same number to i and run through the failed iteration of the loop again? 回答1: Do a while True inside your for loop, put your try code inside, and break from that while loop only when your code succeeds.

Weird Try-Except-Else-Finally behavior with Return statements

梦想与她 提交于 2019-11-26 02:55:49
问题 This is some code that is behaving peculiarly. This is a simplified version of the behavior that I\'ve written. This will still demonstrate the weird behavior and I had some specific questions on why this is occurring. I\'m using Python 2.6.6 on Windows 7. def demo1(): try: raise RuntimeError,\"To Force Issue\" except: return 1 else: return 2 finally: return 3 def demo2(): try: try: raise RuntimeError,\"To Force Issue\" except: return 1 else: return 2 finally: return 3 except: print 4 else:

How to properly ignore exceptions

北城以北 提交于 2019-11-25 22:36:15
问题 When you just want to do a try-except without handling the exception, how do you do it in Python? Is the following the right way to do it? try: shutil.rmtree(path) except: pass 回答1: try: doSomething() except: pass or try: doSomething() except Exception: pass The difference is, that the first one will also catch KeyboardInterrupt , SystemExit and stuff like that, which are derived directly from exceptions.BaseException , not exceptions.Exception . See documentation for details: try statement —