Is there a cleaner or more pythonic way to do the following?
try:
error_prone_function(arg1)
except MyError:
try:
error_prone_function(arg2)
except MyError:
try:
another_error_prone_function(arg3)
except MyError:
try:
last_error_prone_function(arg4)
except MyError:
raise MyError("All backup parameters failed.")
Basically it's: If attempt #1 fails, try #2. If #2 fails, try #3. If #3 fails, try #4. If #4 fails, ... if #n fails, then finally raise some exception.
Note that I'm not necessarily calling the same function every time, nor am I using the same function arguments every time. I am, however, expecting the same exception MyError on each function.
Thanks to John Kugelman's post here, I decided to go with this which utilizes the lesser-known else clause of a for loop to execute code when an entire list has been exhausted without a break happening.
funcs_and_args = [(func1, "150mm"),
(func1, "100mm",
(func2, "50mm"),
(func3, "50mm"),
]
for func, arg in funcs_and_args :
try:
func(arg)
# exit the loop on success
break
except MyError:
# repeat the loop on failure
continue
else:
# List exhausted without break, so there must have always been an Error
raise MyError("Error text")
As Daniel Roseman commented below, be careful with indentation since the try statement also has an else clause.
A generator based approach might give you a little more flexibility than the data-driven approach:
def attempts_generator():
# try:
# <the code you're attempting to run>
# except Exception as e:
# # failure
# yield e.message
# else:
# # success
# return
try:
print 'Attempt 1'
raise Exception('Failed attempt 1')
except Exception as e:
yield e.message
else:
return
try:
print 'Attempt 2'
# raise Exception('Failed attempt 2')
except Exception as e:
yield e.message
else:
return
try:
print 'Attempt 3'
raise Exception('Failed attempt 3')
except Exception as e:
yield e.message
else:
return
try:
print 'Attempt 4'
raise Exception('Failed attempt 4')
except Exception as e:
yield e.message
else:
return
raise Exception('All attempts failed!')
attempts = attempts_generator()
for attempt in attempts:
print attempt + ', retrying...'
print 'All good!'
The idea is to build a generator that steps through attempt blocks via a retry loop.
Once the generator hits a successful attempt it stops its own iteration with a hard return. Unsuccessful attempts yield to the retry loop for the next fallback. Otherwise if it runs out of attempts it eventually throws an error that it couldn't recover.
The advantage here is that the contents of the try..excepts can be whatever you want, not just individual function calls if that's especially awkward for whatever reason. The generator function can also be defined within a closure.
As I did here, the yield can pass back information for logging as well.
Output of above, btw, noting that I let attempt 2 succeed as written:
mbp:scratch geo$ python ./fallback.py
Attempt 1
Failed attempt 1, retrying...
Attempt 2
All good!
If you uncomment the raise in attempt 2 so they all fail you get:
mbp:scratch geo$ python ./fallback.py
Attempt 1
Failed attempt 1, retrying...
Attempt 2
Failed attempt 2, retrying...
Attempt 3
Failed attempt 3, retrying...
Attempt 4
Failed attempt 4, retrying...
Traceback (most recent call last):
File "./fallback.py", line 47, in <module>
for attempt in attempts:
File "./fallback.py", line 44, in attempts_generator
raise Exception('All attempts failed!')
Exception: All attempts failed!
Edit:
In terms of your pseudocode, this looks like:
def attempts_generator():
try:
error_prone_function(arg1)
except MyError
yield
else:
return
try:
error_prone_function(arg2)
except MyError
yield
else:
return
try:
another_error_prone_function(arg3)
except MyError:
yield
else:
return
try:
last_error_prone_function(arg4)
except MyError:
yield
else:
return
raise MyError("All backup parameters failed.")
attempts = attempts_generator()
for attempt in attempts:
pass
It'll let any exception but MyError bubble out and stop the whole thing. You also could choose to catch different errors for each block.
Related
Is there any way to raise multiple exceptions in python? In the following example, only the first exception is raised.
l = [0]
try:
1 / 0
except ZeroDivisionError as e:
raise Exception('zero division error') from e
try:
l[1]
except IndexError as e:
raise Exception('Index out of range') from e
Is there any other way?
Once an exception is raised and not catched, the execution of your program will stop. Hence, only one exception can be launched in such a simple script.
If you really want your program to raise multiple exceptions, you could however create a custom Exception representing multiple exceptions and launch it inside an except block. Example:
class ZeroDivAndIndexException(Exception):
"""Exception for Zero division and Index Out Of Bounds."""
I = [0]
try:
1 / I[0]
except ZeroDivisionError as e:
try:
I[1]
# Here you may raise Exception('zero division error')
except IndexError as e2:
raise ZeroDivAndIndexException()
Here my solution a bit long but seems to work :
class CustomError(Exception):
pass
l = [0]
exeps = []
try:
1 / 0
except ZeroDivisionError as e:
exeps.append(e)
try:
l[1]
except IndexError as e:
exeps.append(e)
if len(exeps)!=0:
[print(i.args[0]) for i in exeps]
raise CustomError("Multiple errors !!")
I want to get only the exception message,not the entire stack trace.A toy example would be as follows:
def div():
try:
print(0/0)
except:
raise Exception('Divide by zero,please check the input!!!')
def func1():
try:
div()
except:
raise Exception('Error in func11 try')
func1()
Expected output
Exception: Divide by zero, please check the input!!!
Exception: Error in func11 try
Any help is highly appreciated.
Below solution worked for me.
import traceback
def div():
try:
print(0/0)
except:
raise Exception('Divide by zero,please check the input!!!')
def func2():
try:
div()
except:
raise Exception('Error in func 2!!!')
def func1():
exceptions=[]
try:
func2()
except Exception as e:
msg=traceback.format_exc()
msg_list= msg.split('\n')
[exceptions.append(line) for line in msg_list if line.startswith('Exception:')]
return exceptions
func1()
output
['Exception: Divide by zero,please check the input!!!',
'Exception: Error in func 2!!!']
If you don't want to get the entire stack trace, you can try this:
def div():
try:
print(0/0)
except Exception as e:
print(e)
The answer of #Kairav Parekh works in your condition and it does not prints the entire stack trace, it only prints the error, which is user-friendly in most cases,
you only need to modify his code to match your requirements and you can put a break statement in except such as below,
def div():
try:
print(0/a)
except Exception as e:
print(e)
break #break here
div()
Say I have a block of exception statements:
try:
expression
except err1:
#process error
...
...
except err10:
#process error
and I want to call sys.exit(1) if ANY of the exceptions are raised. Do I have to call it manually in every single sub-block or is there a built in way to have a statement akin to:
...
except err10:
#process error
"if any of these exception were raised":
sys.exit(1)
One thing you could do is:
flag = False
try:
expression
flag = True
except err1:
#process error
...
...
except err10:
#process error
if not flag:
sys.exit(1) #exit program
If the flag is False, that means that you didn’t pass through the try loop, and so an error was raised.
In Python, there is an optional else block which is executed in case no exception is raised. You may use this to set a flag for you code and exit the code out of the try/except block as:
is_exception = True
try:
expression
except err1:
# ... something
except err10:
# ... something else
else:
# This will be executed if there is no exception
is_exception = False
if is_exception:
sys.exit(1)
raised = True
try:
expression
except err1:
# process error
raise
...
except err10:
# process error
raise
else:
# if no error was raised
raised = False
finally:
if raised:
raise SystemExit
Here's what I was talking about in my comment:
isok = False
try:
#try to do something
isok = True
except err1:
#do something besides raising an exception
except err5:
#do something besides raising an exception
if not isok:
raise SystemExit
Objective: I have several lines of code each capable of producing the same type of error, and warranting the same kind of response. How do I prevent a 'do not repeat yourself' problem with the try-except blocks.
Background:
I using ReGex to scrape poorly formatted data from a text file, and input it into the field of a custom object. The code works great except when the field has been left blank in which case it throws an error.
I handle this error in a try-except block. If error, insert a blank into the field of the object (i.e. '').
The problem is it turns easily readable, nice, Python code into a mess of try-except blocks that each do exact same thing. This is a 'do not repeat yourself' (a.k.a. DRY) violation.
The Code:
Before:
sample.thickness = find_field('Thickness', sample_datum)[0]
sample.max_tension = find_field('Maximum Load', sample_datum)[0]
sample.max_length = find_field('Maximum Extension', sample_datum)[0]
sample.test_type = sample_test
After:
try:
sample.thickness = find_field('Thickness', sample_datum)[0]
except:
sample.thickness = ''
try:
sample.max_tension = find_field('Maximum Load', sample_datum)[0]
except:
sample.max_tension = ''
try:
sample.max_length = find_field('Maximum Extension', sample_datum)[0]
except:
sample.max_length = ''
try:
sample.test_type = sample_test
except:
sample.test_type = ''
What I Need:
Is there some Pythonic way to write this? Some block where I can say if there is an index-out-of-range error on any of these lines (indicating the field was blank, and ReGex failed to return anything) insert a blank in the sample field.
What about extracting a function out of it?
def maybe_find_field(name, datum):
try:
return find_field(name, datum)[0]
except IndexError: # Example of specific exception to catch
return ''
sample.thickness = maybe_find_field('Thickness', sample_datum)
sample.max_tension = maybe_find_field('Maximum Load', sample_datum)
sample.max_length = maybe_find_field('Maximum Extension', sample_datum)
sample.test_type = sample_test
BTW, don't simply catch all possible exceptions with except: unless that's really what you want to do. Catching everything may hide implementation errors and become quite difficult to debug later. Whenever possible, bind your except case to the specific exception that you need.
Not quite matching the question, but Google sent me here and so others might come.
I have post functions in two separate Django-views, which call similar backend-functions and require the same exception-handling.
I solved the issue by extracting the whole except:-tree and sticking it into a decorator.
Before:
# twice this
def post(request):
try:
return backend_function(request.post)
except ProblemA as e:
return Response("Problem A has occurred, try this to fix it.", status=400)
except ProblemB as e:
return Response("Problem B has occurred, try this to fix it.", status=400)
except ProblemC as e:
return Response("Problem C has occurred, try this to fix it.", status=400)
except ProblemD as e:
return Response("Problem D has occurred, try this to fix it.", status=400)
After:
# once this
def catch_all_those_pesky_exceptions(original_function):
def decorated(*args, **kwargs):
try:
return original_function(*args, **kwargs)
except ProblemA as e:
return Response("Problem A has occurred, try this to fix it.", status=400)
except ProblemB as e:
return Response("Problem B has occurred, try this to fix it.", status=400)
except ProblemC as e:
return Response("Problem C has occurred, try this to fix it.", status=400)
except ProblemD as e:
return Response("Problem D has occurred, try this to fix it.", status=400)
return decorated
# twice this - note the #decorator matching the above function.
#catch_all_those_pesky_exceptions
def post(request):
return backend_function(request.post)
Now I can add more exception-handling in a single place.
When you find yourself repeating code, encapsulate it in a function. In this case, create a function that handles the exception for you.
def try_find_field(field_name, datum, default_value):
try:
return find_field(field_name, datum)[0]
except:
return default_value
What about something like this:
def exception_handler(exception_class):
logger = logging.getLogger('app_error')
logger.error(exception_class)
exception_name = exception_class.__name__
if exception_name == 'AuthenticationError':
raise AuthenticationError
elif exception_name == 'AuthorizationError':
raise AuthorizationError
elif exception_name == 'ConnectionError':
raise ConnectionError
else:
raise GenericError
def call_external_api()
try:
result = http_request()
except Exception as e:
exception_handler(exception_class=type(e))
You can have any number of except blocks over and over, handling different kinds of exceptions. There's also nothing wrong with having multiple statements in the same try/catch block.
try:
doMyDangerousThing()
except ValueError:
print "ValueError!"
except HurrDurrError:
print "hurr durr, there's an error"
try:
doMyDangerousThing()
doMySecondDangerousThing()
except:
print "Something went wrong!"
Will this work?
try:
try:
field.value = filter(field.value, fields=self.fields, form=self, field=field)
except TypeError:
field.value = filter(field.value)
except ValidationError, e:
field.errors += e.args
field.value = revert
valid = False
break
Namely, if that first line throws a ValidationError, will the second except catch it?
I would have written it un-nested, but the second filter statement can fail too! And I want to use the same ValidationError block to catch that as well.
I'd test it myself, but this code is so interwoven now it's difficult to trip it properly :)
As a side note, is it bad to rely on it catching the TypeError and passing in only one arg instead? i.e., deliberately omitting some arguments where they aren't needed?
If the filter statement in the inner try raises an exception, it will first get checked against the inner set of "except" statements and then if none of those catch it, it will be checked against the outer set of "except" statements.
You can convince yourself this is the case just by doing something simple like this (this will only print "Caught the value error"):
try:
try:
raise ValueError('1')
except TypeError:
print 'Caught the type error'
except ValueError:
print 'Caught the value error!'
As another example, this one should print "Caught the inner ValueError" only:
try:
try:
raise ValueError('1')
except TypeError:
pass
except ValueError:
print 'Caught the inner ValueError!'
except ValueError:
print 'Caught the outer value error!'
To compliment Brent's answer, and test the other case:
class ValidationError(Exception): pass
def f(a): raise ValidationError()
try:
try:
f()
except TypeError:
f(1)
except ValidationError:
print 'caught1'
try:
try:
f(1)
except TypeError:
print 'oops'
except ValidationError:
print 'caught2'
Which prints:
caught1
caught2