I am implementing a parser for a domain specific language, and want to be able to raise a SyntaxError. How do I set the filename, lineno and offset when raising this exception?
exception SyntaxError
Raised when the parser encounters a syntax error. This may occur in an import statement, in an exec statement, in a call to the built-in function eval() or input(), or when reading the initial script or standard input (also interactively).
Instances of this class have attributes filename, lineno, offset and text for easier access to the details. str() of the exception instance returns only the message.
Source: https://docs.python.org/3.2/library/exceptions.html#SyntaxError
SyntaxError
, just your ownException
subclass (e.g.class MySyntaxError(Exception):
) that you can add whatever attributes you like to. – Vanhomrigh