exception-handling

Handle specific exception from python package

Handle specific exception from python package Question: I would like to handle the following Exception from py_vollib/py_lets_be_rational in specific way. py_lets_be_rational.exceptions.BelowIntrinsicException: The volatility is below the intrinsic value. Tried this without success: from py_vollib.black.implied_volatility import implied_volatility as impl_vol_b from py_lets_be_rational.exceptions import BelowIntrinsicException try: call_vol = impl_vol_b(discounted_option_price, F, K, r, t, type) except BelowIntrinsicException as e: …

Total answers: 2

Does 'finally' always execute in Python?

Does 'finally' always execute in Python? Question: For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed? For example, let’s say I return while in an except block: try: 1/0 except ZeroDivisionError: return finally: print(“Does this code run?”) Or maybe I re-raise an Exception: try: 1/0 except …

Total answers: 6

Catching KeyError message in Python 3

Catching KeyError message in Python 3 Question: I’m having trouble catching the message in a KeyError in Python 3 (More specifically 3.5.3). Input: try: raise KeyError(‘some error message’) except KeyError as e: if str(e) == ‘some error message’: print(‘Caught error message’) else: print(“Didn’t catch error message”) Output: Didn’t catch error message Oddly, the same thing …

Total answers: 3

Simple Python Logging Exception from Future

Simple Python Logging Exception from Future Question: This should be a really simple question, but after googling, reading docs, and several other SO threads, I don’t see the answer: How do I log an exception with Python standard logging? One small wrinkle is that I’m getting the exception from a Future. I’m not writing the …

Total answers: 1

Avoiding "Too broad exception clause" warning in PyCharm

Avoiding "Too broad exception clause" warning in PyCharm Question: I’m writing an exception clause at the top level of a script, and I just want it to log whatever errors occur. Annoyingly, PyCharm complains if I just catch Exception. import logging logging.basicConfig() try: raise RuntimeError(‘Bad stuff happened.’) except Exception: # <= causes warning: Too broad …

Total answers: 4

One-Line Exception Handling

One-Line Exception Handling Question: In Python, it is possible to use one-liners to set values with special conditions (such as defaults or conditions) in a simple, intuitive way. result = 0 or “Does not exist.” # “Does not exist.” result = “Found user!” if user in user_list else “User not found.” Is it possible to …

Total answers: 4

finally and rethowing of exception in except, raise in python

finally and rethowing of exception in except, raise in python Question: try: #error code except Exception as e: print ‘error’,e raise miexp(“malicious error”) #userdefined exception, miexp finally: print ‘finally’ Why the output is in the following formats? Output: error finally malicious error Actually I expected as: error malicious error finally Why so? Asked By: Maran …

Total answers: 1

How to get exception message in Python properly

How to get exception message in Python properly Question: What is the best way to get exceptions’ messages from components of standard library in Python? I noticed that in some cases you can get it via message field like this: try: pass except Exception as ex: print(ex.message) but in some cases (for example, in case …

Total answers: 5

Can't catch mocked exception because it doesn't inherit BaseException

Can't catch mocked exception because it doesn't inherit BaseException Question: I’m working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of different exceptions, and behave differently depending on which exception is caught. For example: def myMethod(address, timeout=20): …

Total answers: 6