Prev: ftp and python
Next: daemon.DaemonContext
From: Gabriel Genellina on 7 Apr 2010 23:52 En Wed, 07 Apr 2010 17:23:22 -0300, kwatch <kwatch(a)gmail.com> escribi�: > Is it possible to raise exception with custom traceback to specify > file and line? > I'm creating a certain parser. > I want to report syntax error with the same format as other exception. > ------------------------- > 1: def parse(filename): > 2: if something_is_wrong(): > 3: linenum = 123 > 4: raise Exception("syntax error on %s, line %s" % (filename, > linenum)) > 5: > 6: parse('example.file') > ------------------------- > > my hope is: > ------------------------- > Traceback (most recent call last): > File "/tmp/parser.py", line 6, in <module> > parse('example.file') > File "/tmp/parser.py", line 4, in parse > raise Exception("syntax error on %s, line %s" % (filename, > linenum)) > File "/tmp/example.file", line 123 > foreach item in items # wrong syntax line > Exception: syntax error > ------------------------- 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
From: kwatch on 9 Apr 2010 22:19 On 4æ8æ¥, åå¾12:52, "Gabriel Genellina" <gagsl-...(a)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
From: Gabriel Genellina on 12 Apr 2010 14:23 En Fri, 09 Apr 2010 23:19:48 -0300, kwatch <kwatch(a)gmail.com> escribió: > On 4月8日, 午後12:52, "Gabriel Genellina" <gagsl-...(a)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
|
Pages: 1 Prev: ftp and python Next: daemon.DaemonContext |