exception

Exception Handling with subprocess.run in Python

Exception Handling with subprocess.run in Python Question: I am trying to create a function that can run any shell command and return the stdout of that command, without worrying about any exceptions that could be thrown. When testing the code written below with incorrect commands like xyz testing, I get a FileNotFoundError rather that the …

Total answers: 1

Slack Notification for Python try, except method

Slack Notification for Python try, except method Question: I am trying to write a python code with the try and except method where anytime an exception occurs it should give me a slack notification with the exception occurred. Here is the code that I have written: try: with MailBox(‘imap.gmail.com’).login(username, password) as mailbox: print(mailbox) except Exception …

Total answers: 1

How can we raise a HTTPError exception?

How can we raise a HTTPError exception? Question: I want to raise an exception whenever i get data value as None. How can we achieve it without using custom exception? Ex: def foo(): <some code> return data @store.route("/store", methods=["GET"]) def fun(): try: data = foo() if not data: raise HTTPError() except HTTPError as he: return …

Total answers: 2

FastAPI/Starlette: How to handle exceptions inside background tasks?

FastAPI/Starlette: How to handle exceptions inside background tasks? Question: I developed some API endpoints using FastAPI. These endpoints are allowed to run BackgroundTasks. Unfortunately, I do not know how to handle unpredictable issues from theses tasks. An example of my API is shown below: # main.py from fastapi import FastAPI import uvicorn app = FastAPI() …

Total answers: 2

Python2 vs Python3 : Exception variable not defined

Python2 vs Python3 : Exception variable not defined Question: I wrote this small snippet in python2 (2.7.18) to catch the exception in a variable and it works >>> ex = None >>> try: … raise Exception("test") … except Exception as ex: … print(ex) … test >>> ex Exception(‘test’,) >>> >>> ex2 = None >>> try: …

Total answers: 1

how do i raise exception when i promp user to enter a number

how do i raise an exception when i prompt user to enter a number Question: i want to get input from user to fill the empty list. if user enters a negative integer- it will not get appended rather a ValueError will be raised. nameList = [] count = 0 try: while count < 5: …

Total answers: 3

Create a Category Tree using python

Create a Category Tree using python Question: A category tree is a representation of a set of categories and their parent-child relationships. Each category has a unique name (no two categories have the same name). A category can have a parent category. Categories without a parent category are called root categories. Need to create a …

Total answers: 1

Get file path from Tkinter filedialog not working

Get file path from Tkinter filedialog not working Question: Working on a project where I have a function that shows an open file dialog and prints out the path to the selected file. My code looks like this: import tkinter import customtkinter def openFile(self): filePath = tkinter.filedialog.askopenfile(initialdir=startingDir, title="Open File", filetypes=(("Open a .txt file", "*.txt"), ("All …

Total answers: 1

What exactly happens when you create an alias of the Exception class?

What exactly happens when you create an alias of the Exception class? Question: try: 0/0 except Exception as e: print(e) The above code prints division by zero as one would expect. But if we try to print without creating the alias: try: 0/0 except Exception: print(Exception) It simply prints <class ‘Exception’>. What is happening here? …

Total answers: 1