En Fri, 09 Apr 2010 23:19:48 -0300, kwatch <kwa...@gmail.com> escribió:

On 4月8日, 午後12:52, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:

The built-in SyntaxError exception does what you want. Constructor parameters are undocumented, but they're as follows:

raise SyntaxError("A descriptive error message", (filename, linenum, colnum, source_line))

colnum is used to place the ^ symbol (10 in this fake example). Output:

Traceback (most recent call last):
   File "1.py", line 9, in <module>
     foo()
   File "1.py", line 7, in foo
raise SyntaxError("A descriptive error message", (filename, linenum, colnum, "this is line 123 in example.file"))
   File "example.file", line 123
     this is line 123 in example.file
              ^
SyntaxError: A descriptive error message


By the way, is it hard to specify any other exception class instead of
SyntaxError?
The SyntaxError class is a good solution in my case, but if possible,
I want to know
more general solution to specify filename and linenum for exception.

You can always store any info you want in the exception object, just write __str__ accordingly. The advantage of SyntaxError is that it is special-cased in the interpreter itself as to add some spaces and the ^ character. The disadvantages are already pointed out by Dennis Lee Bieber and aren't minor.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to