Questions tagged [try-except]

A form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Try/Except is a form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Links

Error Handling in Python
Except Keyword in Delphi
Try/Except in Microsoft C and C++

898 questions
941
votes
12 answers

How to properly ignore exceptions

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
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
405
votes
30 answers

How to retry after exception?

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…
FurtiveFelon
  • 14,714
  • 27
  • 76
  • 97
118
votes
3 answers

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

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…
Kyle Owens
  • 1,321
  • 2
  • 9
  • 10
117
votes
4 answers

Python try finally block returns

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…
skybobbi
  • 1,486
  • 2
  • 10
  • 7
92
votes
3 answers

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

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,…
user3348051
64
votes
3 answers

Try-except clause with an empty except code

Sometimes you don't want to place any code in the except part because you just want to be assured of a code running without any error but not interested to catch them. I could do this like so in C#: try { do_something() }catch (...) {} How could I…
Ehsan88
  • 3,569
  • 5
  • 29
  • 52
52
votes
2 answers

python 3 try-except all with error

Is it possible to do a try-except catch all that still shows the error without catching every possible exception? I have a case where exceptions will happen once a day every few days in a script running 24/7. I can't let the script die but they also…
Ryan Mills
  • 939
  • 1
  • 10
  • 23
48
votes
3 answers

Try/except when using Python requests module

Doing some API testing and trying to create a function that given an inputted URL it will return the json response, however if a HTTP error is the response an error message will be returned. I was using urllib2 before, but now trying to use…
mroriel
  • 605
  • 1
  • 5
  • 7
30
votes
7 answers

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

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; …
user741875
24
votes
2 answers

syntaxError: 'continue' not properly in loop

I have been struggling with this error for a while now and there seems to be different opinions regarding why the interpreter complains about the 'continue'. So I would like to provide the erroneous code below. import tweepy import time def…
anonuser0428
  • 11,789
  • 22
  • 63
  • 86
23
votes
2 answers

Print exception with stack trace to file

I'm trying to put a simple log into my script. This log should tell me where is the error and as much as possible info needed to repair the script. I've put print to file str(e) into each except but it provides a very few info to know what is going…
Milano
  • 18,048
  • 37
  • 153
  • 353
22
votes
5 answers

What's the correct Try Exception for NoneType when using regex's .groups() function

I'm trying to use the following code: try: clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups()) except TypeError: clean = "" However I get the following traceback... Traceback (most recent call last): File "test.py", line…
Ryflex
  • 5,559
  • 25
  • 79
  • 148
19
votes
1 answer

Why do I get a `NameError` (or `UnboundLocalError`) from using a named exception after the `except` block?

This example code worked in 2.x: exc = None try: raise Exception except Exception as exc: pass print(exc) But in 3.x I get an error that says NameError: name 'exc' is not defined. If I put the code in a function instead, I get…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
18
votes
2 answers

Python Selenium Webdriver - Try except loop

I'm trying to automate processes on a webpage that loads frame by frame. I'm trying to set up a try-except loop which executes only after an element is confirmed present. This is the code I've set up: from selenium.common.exceptions import…
user3294195
  • 1,748
  • 1
  • 19
  • 36
17
votes
5 answers

Python get an error code from exception

In python, I have code that handles exceptions and prints error codes and messages. try: somecode() #raises NameError except Exception as e: print('Error! Code: {c}, Message, {m}'.format(c = e.code, m = str(e)) However, e.code is not the…
Pythonic Guy 21421
  • 371
  • 1
  • 3
  • 8
1
2 3
59 60