All Questions
Tagged with python exceptions
39 questions
4
votes
2
answers
431
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 ...
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 ...
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 ...
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 ...
-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 ...
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 ...
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
...
1
vote
1
answer
114
views
Should exception-blocks handle only exceptions raised from try-blocks?
Should the exception blocks in a try-except sequence be specific only to those exceptions which may originate from the try? Or can they be inclusive of exceptions that may arise from handling the ...
2
votes
2
answers
2k
views
What is the difference between unit testing and handling exceptions
I have spent two full days now trying to understand the difference between unit testing and handling exception, but I can't get it.
Things I have understood (or I think I have):
Unit testing tests ...
3
votes
2
answers
2k
views
Exceptions versus guard clauses
I'm trying to get my head around Python exceptions. I've read quite a bit on the topic but can't get clear answers to some questions. In particular, I'm still not sure whether to use exceptions or ...
1
vote
1
answer
714
views
Best practices for handling application specific exceptions?
Is it considered to be a good practice to convert all types of exceptions (exceptions from internal logic of application + exceptions from application's external dependencies - for example: File ...
1
vote
1
answer
189
views
Is raising an unrelated exception for convenience acceptable? [duplicate]
Let's say I have a function that returns a dataset. First it tries to read it and if that fails it is requested from an API and then written:
def get_dataset():
try:
df = pd.read_csv('...