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
3
votes
3 answers

Python Exceptions Control/Flow Issue

I've been working in Python and ran into something that must be a common occurrence. I have five statements that all fall into a common pitfall of raising FooException and BarException. I want to run each of them, guarding against these exceptions…
3
votes
1 answer

Cannot catch requests.exceptions.ConnectionError with try except

It feels like I am slowly losing my sanity. I am unable to catch a connection error in a REST-API request. I read at least 20 similar questions on stackoverflow, tried every possible except statement I could think of and simplified the code as much…
Tilman
  • 141
  • 1
  • 10
3
votes
2 answers

try block is working but except block is not handling error in python

While Handling Python dictionary key error except block is not working but try block is working. Below is my code def catch_empty_key(a): try: return 'aaaa' except : return 'bbbb' def zohoapicall(accesstoken): …
3
votes
3 answers

Python Nested Recursion for multi condition exception handling

I have 3 conditions C1,C2,C3 which are to be checked. If C1 gives a timeout exception, try C2. If C2 gives a timeout exception, try C3. If C3 gives timeout Exception, return None for the function. But if any of C1, C2, C3 is success, execute rest of…
3
votes
2 answers

How to avoid duplicates when python multiple except executes the same method?

I have the code block like below: try: method() except ErrorType1: todo() return except ErrorType2 as e: todo() raise e Basically for the two error types, I need to execute todo() first, then either return or raise e. Is it possible…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
3
votes
1 answer

How to search for an element and incase it does not exist search for another element using Selenium Python

I'm trying to search for an element and if it doesn't exist search for another element, and here is my code: try: elem1 = driver.find_element_by_xpath('xpath1') elem = driver.find_element_by_xpath("xpath2") if elem.is_displayed(): …
3
votes
5 answers

How to repeat the input until a special condition is met in Python?

I need to take integer inputs from the user and add them to a set. The number of integers is unknown. The input process will end when the user input is "Done". Here is my code: s = set() print('Please type the number, when you are done please type…
3
votes
2 answers

Python try except, try again after executing except statement

I'm trying to execute a statement, but if it fails I want to execute the except statement and after I want to execute the try again. I know I can use loops, but I'm looking for a more elegant solution. In my use case I try to save a file to a folder…
Hestaron
  • 190
  • 1
  • 8
3
votes
3 answers

What is the best way to execute code when a test does not raise an error in Python?

Background story: I am processing some messy data sheets made in Excel. Reading this with pandas (preferable) does sometimes conserve the Excel data type of the cells. As all entries I would like to process have a value in a certain column that can…
Jan Willem
  • 820
  • 1
  • 6
  • 24
3
votes
1 answer

Can I throw a custom exception within except clause without causing: "During handling of..."

So first of all, if this isn't best practice in your opinion I'd love to discuss that in the comments. My problem is as follows, I try to find a file based on user input. If the file cannot be found I get a FileNotFoundError. Which is great, but I…
Kerwin Sneijders
  • 750
  • 13
  • 33
3
votes
3 answers

How do I use try except on multiple elements?

So I'm trying to set several values divided by other variables that could be 0, so I decided to use try except: try: p = pacific / float(a) m = mountain / float(b) c = central / float(c) e = eastern / float(d) …
dl784
  • 61
  • 4
3
votes
2 answers

Catch a Specific OSError Exception in Python 3

In Python 3, how can we catch a specific OSError exception? My current code catches all OSError, but only OSError: [Errno 12] needs to be caught. try: foo() except OSError as e: print('Caught OSError: [Errno12]') The full error message…
Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60
3
votes
3 answers

How to use try-except in Python function return boolean?

I need to use try-except to validate the input string of a date and time.The input format should be MM/DD/YYYY HH:MM:SS. If the validate function returns false, then it's not valid.I tried a invalid input, but the try-except didn't work as expected.…
Timon
  • 185
  • 2
  • 4
  • 10
3
votes
0 answers

Python try/except - best practices / optimisation

I'm looking for some information about best practices for try/except block in Python. I've meet few different approaches but I cannot find any proves/docs that one of them is the best to use. Which one has the best performance and why? All i one…
kjago
  • 69
  • 3
3
votes
1 answer

Python docs have misleading explanation of return in finally

I was going through the python docs to improve my core python and I was reading about errors and exceptions In the doc it says If a finally clause includes a return statement, the finally clause’s return statement will execute before, and instead…
Eternal
  • 928
  • 9
  • 22