exception

Python: Except block capturing exception that is not listed for block

Python: Except block capturing exception that is not listed for block Question: I have a function that makes a request to AWS using boto3, and I wish to capture any exceptions and raise one of two custom errors, depending on the botocore.exceptions exception that is returned. I thought I had this working, but an except …

Total answers: 1

Unable to Catch Exception and Raise Error in Python

Unable to Catch Exception and Raise Error in Python Question: Suppose that I have a variable initial_cash = ‘a’. I want to check if this is convertible to float else I want to raise error for that I’ve written code below: initial_cash = ‘a’ try: initial_cash = float(initial_cash) except TypeError: raise TypeError("Initial cash amount should …

Total answers: 1

Python Exception Handling – BMI CALCULATOR

Python Exception Handling – BMI CALCULATOR Question: I am coding a BMI calculator in python and wanted to add exception handling. I created two functions 1. a height converter that converts the height to feet or meters depending on the user’s input, and a weight converter that converts the user’s weight to kg or pounds …

Total answers: 2

Raise a python exception without "raise" statement

Raise a python exception without "raise" statement Question: Is it possible to raise an exception in Python without using raise statement? For example, instead of raise ValueError use some method like ValueError.raise(). This question only relates to python built-in exceptions and not some custom exception classes that can be build from them. Asked By: Max …

Total answers: 2

How do you obtain underlying failed request data when catching requests.exceptions.RetryError?

How do you obtain underlying failed request data when catching requests.exceptions.RetryError? Question: I am using a somewhat standard pattern for putting retry behavior around requests requests in Python, import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry retry_strategy = Retry( total=HTTP_RETRY_LIMIT, status_forcelist=HTTP_RETRY_CODES, method_whitelist=HTTP_RETRY_METHODS, backoff_factor=HTTP_BACKOFF_FACTOR ) adapter = HTTPAdapter(max_retries=retry_strategy) http = requests.Session() http.mount("https://", adapter) http.mount("http://", …

Total answers: 3

Gracefully exiting a child thread on uncaught main thread exception

Gracefully exiting a child thread on uncaught main thread exception Question: I have a setup with a worker thread that looks as follows: from time import sleep from threading import Event, Thread class MyThread(Thread): def __init__(self, *args, **kwargs): # Following Doug Fort: "Terminating a Thread" # (https://www.oreilly.com/library/view/python-cookbook/0596001673/ch06s03.html) self._stop_request = Event() super().__init__(*args, **kwargs) def run(self): while …

Total answers: 1

Correct raise translation for python 3.7 w two arguments

Correct raise translation for python 3.7 w two arguments Question: How do I convert this code from Python 2 to to Python 3: except ET.XMLSyntaxError, log: #This exception raised if the file has parse errors in it logging.error("XSD: " + log.message) raise SyntaxError, log.message I’m not actually sure what the raise line is doing. I’ve …

Total answers: 1

while loop could not break

while loop could not break Question: there is NameError: name ‘user’ is not defined why while loop is not ending please help to find the problem this program is for dice roll in pyhton I want to roll the dice with no exception but there are exception accured rand_num=random.randint(1, 6) game_is_on=True while True: try: user=int(input("What’s …

Total answers: 3

Handling keyboard interrupt and wondering if all my variables are updated together

Handling keyboard interrupt and wondering if all my variables are updated together Question: So I am wondering how exactly the keyboard interrupt relates to variable assignments. I want to know specifically if in the following 2 examples there would be a difference. The difference I am expecting is that in the second case all variables …

Total answers: 2

Stop code execution if a certain value is not present in a column

Stop code execution if a certain value is not present in a column Question: I want only certain values to be filled in a column (from a list A-H) and if the value is not present in that list then the code should thrown an error and stop executing further. res = APAC[‘colA’].isin([‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’]) try: for …

Total answers: 1