try:
do_something()
except:
pass
You will use the pass statement.
Answer from Andy on Stack OverflowThe pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
try:
do_something()
except:
pass
You will use the pass statement.
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
Use pass:
try:
foo()
except:
pass
A pass is just a placeholder for nothing, it just passes along to prevent SyntaxErrors.
Is it OK to catch an exception and do nothing, if the exception is expected?
How to catch empty user input using a try and except in python? - Stack Overflow
Is it okay to leave except EMPTY, when I print out trackback anyway? [Python 3.10] - Stack Overflow
exception - empty error message in python - Stack Overflow
I think you are accurate about the trade-off involved - neither is ideal. Out of those two, I personally prefer the first one since the control flow is easier to follow.
That said, I'd suggest considering a third option that you didn't mention: factor out some or all of the recovery logic into a separate function. That keeps the control flow simple and avoids having a big block of recovery code in the except clause.
Explicit is better than implicit.
If you are handling an exception, handle it. Right there, right then.
I'm struggling to word this properly but here goes anyway.
If we know a specific exception will be thrown under certain circumstances, is it OK to catch that exception and not tell the user or log it anywhere? Or should an exception always be logged somewhere?
For example, let's say KeyError is expected under certain circumstances, e.g. if a dictionary that is created programmatically doesn't contain an item, yet. In one of my apps, I have that exact situation because the item that will eventually reside there hasn't been extracted from my XML file, yet. Until that time, I'm catching the KeyError, and carrying on knowing the KeyError eventually won't be thrown when I finish processing the XML. Terrible explanation, but I hope it makes sense.
Is that OK or would it be better practice to initialise the dictionary first, say in the class __init__ method so that the KeyError never happens, ever?
Thanks
If you literally want to raise an exception only on the empty string, you'll need to do that manually:
try:
user_input = input() # raw_input in Python 2.x
if not user_input:
raise ValueError('empty string')
except ValueError as e:
print(e)
But that "integer input" part of the comment makes me think what you really want is to raise an exception on anything other than an integer, including but not limited to the empty string.
If so, open up your interactive interpreter and see what happens when you type things like int('2'), int('abc'), int(''), etc., and the answer should be pretty obvious.
But then how do you distinguish an empty string from something different? Simple: Just do the user_input = input() before the try, and check whether user_input is empty within the except. (You put if statements inside except handlers all the time in real code, e.g., to distinguish an OSError with an EINTR errno from one with a different errno.)
try:
input = raw_input('input: ')
if int(input):
......
except ValueError:
if not input:
raise ValueError('empty string')
else:
raise ValueError('not int')
try this, both empty string and non-int can be detected. Next time, be specific of the question.
This means the exception has no message attached. Print the exception type:
print repr(e)
You may also want to print the traceback:
import traceback
# ...
except BaseException as e:
traceback.print_exc()
You want to avoid catching BaseException however, this is no better than a blanket except: statement. Catch more specific exceptions instead.
The following produces a blank line of output:
try:
raise Exception()
except BaseException, e:
print str(e)
Use repr(e) to see what the exception is that was raised.