I've seen similar questions to this one but none of them really address the trackback. If I have a class like so
class Stop_if_no_then():
def __init__(self, value one, operator, value_two, then, line_or_label, line_number):
self._firstvalue = value_one
self._secondvalue = value_two
self._operator = operator
self._gohere = line_or_label
self._then = then
self._line_number = line_number
def execute(self, OtherClass):
"code comparing the first two values and making changes etc"
What I want my execute method to be able to do is if self._then is not equal to the string "THEN" (in allcaps) then I want it to raise a custom error message and terminate the whole program while also not showing a traceback.
If the error is encountered the only thing that should print out would look something like (I'm using 3 as an example, formatting is not a problem) this.
`Syntax Error (Line 3): No -THEN- present in the statement.`
I'm not very picky about it actually being an exception class object, so there's no issue in that aspect. Since I will be using this in a while loop, simple if, elif just repeats the message over and over (because obviously I am not closing the loop). I have seen sys.exit() but that also prints out a giant block of red text, unless I am not using it correctly. I don't want to catch the exception in my loop because there are other classes in the same module in which I need to implement something like this.
The cleanest way that I know is to use
sys.excepthook
.You implement a three argument function that accepts
type
,value
, andtraceback
and does whatever you like (say, only prints the value) and assign that function tosys.excepthook
.Here is an example:
This is available in both python 2 and python 3.
You can use a
try:
and thenexcept Exception as inst:
What that will do is give you your error message in a variable named inst and you can print out the arguments on the error withinst.args
. Try printing it out and seeing what happens, and is any item ininst.args
is the one you are looking for.EDIT Here is an example I tried with pythons IDLE:
EDIT 2: as for closing the program you can always
raise
and error or you can usesys.exit()
If you want to get rid of any traceback for customs exceptions and have line number, you can do this trick
Python 3
Will give this output, and make the
raise
keyword uselessYou can turn off the traceback by limiting its depth.
Python 2.x
Python 3.x
In Python 3.5.2 and 3.6.1, setting
tracebacklimit
to0
does not seem to have the intended effect. This is a known bug. Note that-1
doesn't work either. Setting it toNone
does however seem to work, at least for now.Nevertheless, for better or worse, if multiple exceptions are raised, they can all still be printed. For example:
In general, if you want to catch any exception except
SystemExit
, and exit with the exception's message without the traceback, define yourmain
function as below:Note that in the case of multiple exceptions being raised, only one message is printed.
This answer is meant to improve upon the one by The-IT.