raise exception with fake filename and linenumber

kwatch kwatch at gmail.com
Fri Apr 9 22:19:48 EDT 2010


On 4月8日, 午後12:52, "Gabriel Genellina" <gagsl-... at 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
>
> --
> Gabriel Genellina

Thank you Gabriel,
this is great help for me.

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.

--
makoto



More information about the Python-list mailing list