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.
In Python, A traceback is a report containing the function calls made in your code at a specific point i.e when you get an error it is recommended that you should trace it backward(traceback). Whenever the code gets an exception, the traceback will give the information about what went wrong in the code.
The traceback() function prints the list of functions that were called before the error occurred. The functions are printed in reverse order. traceback() does not tell you where in the function the error occurred.
Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.
You can turn off the traceback by limiting its depth.
import sys sys.tracebacklimit = 0
In Python 3.5.2 and 3.6.1, setting tracebacklimit
to 0
does not seem to have the intended effect. This is a known bug. Note that -1
doesn't work either. Setting it to None
does however seem to work, at least for now.
In Python 3.6.2 and above you should set tracebacklimit
to 0
or -1
, as setting it to None
does not disable the traceback output.
>>> import sys >>> sys.tracebacklimit = 0 >>> raise Exception Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception >>> sys.tracebacklimit = -1 >>> raise Exception Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception >>> sys.tracebacklimit = None >>> raise Exception Exception
>>> import sys >>> sys.tracebacklimit = 0 >>> raise Exception Exception >>> sys.tracebacklimit = -1 >>> raise Exception Exception >>> sys.tracebacklimit = None >>> raise Exception Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception
Nevertheless, for better or worse, if multiple exceptions are raised, they can all still be printed. For example:
socket.gaierror: [Errno -2] Name or service not known During handling of the above exception, another exception occurred: urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With