exception-handling

Manually trigger Django email error report

Manually trigger Django email error report Question: Django error reporting handles uncaught exceptions by sending an email, and (optionally) shows user a nice 500 error page. This works very well, but in a few instances I’d like to allow users to continue with their business uninterrupted, but still have Django send me the email error …

Total answers: 6

Python "raise from" usage

Python "raise from" usage Question: What’s the difference between raise and raise from in Python? try: raise ValueError except Exception as e: raise IndexError which yields Traceback (most recent call last): File “tmp.py”, line 2, in <module> raise ValueError ValueError During handling of the above exception, another exception occurred: Traceback (most recent call last): File …

Total answers: 3

Exception Handling in Pandas .apply() function

Exception Handling in Pandas .apply() function Question: If I have a DataFrame: myDF = DataFrame(data=[[11,11],[22,’2A’],[33,33]], columns = [‘A’,’B’]) Gives the following dataframe (Starting out on stackoverflow and don’t have enough reputation for an image of the DataFrame) | A | B | 0 | 11 | 11 | 1 | 22 | 2A | 2 …

Total answers: 3

Searching for equivalent of FileNotFoundError in Python 2

Searching for equivalent of FileNotFoundError in Python 2 Question: I created a class named Options. It works fine but not not with Python 2. And I want it to work on both Python 2 and 3. The problem is identified: FileNotFoundError doesn t exist in Python 2. But if I use IOError it doesn t …

Total answers: 4

Check if a python thread threw an exception

Check if a python thread threw an exception Question: I have a set of tasks to do in parallel, but at the end of them, I need to know if any of the threads threw an exception. I don’t need to handle the exception directly, I just need to know if one of the threads …

Total answers: 4

Python : Java throws equivalent in python

Python : Java throws equivalent in python Question: Not attempting to compare the languages but just for knowledge, Is there any way to have equivalent of java throws keyword/functionality in Python? or the way we can recognize checked exception thrown by any method at static time? or Passing(chaining) exception handling responsibility? Java: public void someMethod() …

Total answers: 3

Multiple try codes in one block

Multiple try codes in one block Question: I have a problem with my code in the try block. To make it easy this is my code: try: code a code b #if b fails, it should ignore, and go to c. code c #if c fails, go to d code d except: pass Is something …

Total answers: 9

Is it a good practice to use try-except-else in Python?

Is it a good practice to use try-except-else in Python? Question: From time to time in Python, I see the block: try: try_this(whatever) except SomeException as exception: #Handle exception else: return something What is the reason for the try-except-else to exist? I do not like that kind of programming, as it is using exceptions to …

Total answers: 11

Handling all but one exception

Handling all but one exception Question: How to handle all but one exception? try: something except <any Exception except for a NoChildException>: # handling Something like this, except without destroying the original traceback: try: something except NoChildException: raise NoChildException except Exception: # handling Asked By: Ivan Vulović || Source Answers: The answer is to simply …

Total answers: 5

Catch python 'ImportError' if import from source directory

Catch python 'ImportError' if import from source directory Question: When one tries to import a module foo while being in the source directory, one gets an rather confusing ImportError message: ImportError: No module named foo. How can I easily catch this case and return a more informative message, e.g. ‘Please do not load module foo …

Total answers: 1