All Questions
Tagged with python exceptions
39 questions
4
votes
2
answers
434
views
Control flow and communication with two separate frontends (maybe with exceptions)?
I am trying to write a backend for use with a completely text based UI for one shot operations (eg. python scriptname arg, executes that argument and exits) and a GUI using the curses library for some ...
9
votes
4
answers
4k
views
Handling exceptions I don't know about
How do I handle unexpected exceptions without missing any, especially when I don't know all possible exceptions in advance?
When working with exception handling, I often encounter errors I didn’t ...
0
votes
2
answers
602
views
Explain why it's bad to use a middleware to coat error messages as exceptions
We manage a backend application behind a FastAPI REST controller with multiple endpoints.
A member of our team decided to use a middleware on the application which parses the body of the response for ...
7
votes
2
answers
7k
views
What is considered best practice for custom exception classes?
Python has many strong conventions but I'm unclear on how best to manage exceptions for my module. I know it's generally good practice to define a custom exception for your module. E.g.:
class ...
37
votes
4
answers
22k
views
Are exceptions for flow control best practice in Python?
I'm reading "Learning Python" and have come across the following:
User-defined exceptions can also signal nonerror conditions. For
instance, a search routine can be coded to raise an exception ...
3
votes
4
answers
880
views
How to avoid code duplication when else and except are the same?
A simplified version of my code looks like this:
def process( object ):
try:
if suitedForA( object ):
try:
methodA( object )
except:
...
21
votes
4
answers
4k
views
How do non-RAII languages free resources during stack unwinding?
C++ features deterministic order of calling destructors up the call stack until the exception is handled somewhere.
To my knowledge, neither Java nor (object oriented) Python provide this. In those ...
9
votes
2
answers
19k
views
Exception handling in Python - Am I doing this wrong (and why?)
I've read many questions and articles on exception handling in Python (and in general), but I still think that it's the most confusing thing ever. I ended up doing something like this:
# error class ...
1
vote
2
answers
222
views
Is nesting try-except sequence in try-else block bad form?
Ive got a boot sequence that needs to check some registry values, they may or may not be present, so each check needs to be wrapped in its own try-except. I try to avoid nesting as I think it can lead ...
106
votes
9
answers
29k
views
Check First vs Exception Handling?
I'm working through the book "Head First Python" (it's my language to learn this year) and I got to a section where they argue about two code techniques:
Checking First vs Exception handling.
Here is ...
3
votes
1
answer
2k
views
Handling same exception thrown by different methods
Is there an idiomatic way to catch an exception when multiple methods may throw?:
try:
someMethod() # raises OSError
someOtherMethod() # raises OSError
except OSError:
handle()
The ...
-2
votes
4
answers
132
views
Is this a good approach to stop an API function and return relevant error message?
I am writing an API function using DRF where I want the API execution to stop if it fails in any of the steps and return an appropriate response. So I created a custom exception which takes an error ...
32
votes
10
answers
10k
views
What is a good approach to handling exceptions?
I have trouble reconciling "best practices" and real-world approaches to handling exceptions. In my day to day routine, I find myself running into the following examples:
try:
...
0
votes
2
answers
999
views
Code repeated in multiple exception blocks
I'm starting to notice this pattern throughout some of my code:
try:
some_func()
except FirstException as err: # known possible err
# log err
notify_user(err)
except SecondException as ...
-2
votes
2
answers
5k
views
Is using nested try-except blocks problematic?
I've been seeing a lot of this construct throughout my application:
def doSomething():
try:
# setup some variables
try:
# do something that could throw an OSError
...