Thank you.I'm just learning Python and want to make something clear to me.:)
2007/1/20, Steven D'Aprano <[EMAIL PROTECTED]>: > On Sat, 20 Jan 2007 17:30:24 +0800, Jm lists wrote: > > > Hello members, > > > > I want to know does the "eval" in python have the same features as in > > Perl (capture errors)? > > > > For example,in perl I can wrote: > > > > $re = eval { 1 / 0 }; > > > > Though 1/0 is a fatal error but since it's in "eval" block so the perl > > interpreter doesn't get exit. > > How hard would it be to actually try it? > > >>> eval("1/0") > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "<string>", line 1, in <module> > ZeroDivisionError: integer division or modulo by zero > > It took about three seconds to actually test it. > > eval has many security risks -- as a rule, you should never pass strings > you've got from the user to eval, unless you want the user to p0wn your > computer. It is harder than you might think to make eval safe -- you > should seriously consider it an advanced tool, not a basic tool. As a > newbie, you should work under the following rule: > > "If I think I need to use eval, I'm probably wrong." > > I've been using Python for seven or eight years, and I don't think I've > ever used eval in serious code. > > Now, suppose you find yourself wanting to use eval. You've considered the > above rule carefully, and decided that it doesn't apply in this case. > You've been careful to use it only on safe strings, not arbitrary strings > from users. How do you use eval so it captures errors? > > try: > eval("1/0") > except ZeroDivisionError: # capture only one error > pass > > > or something like this: > > try: > eval("something or other goes here") > except Exception: # capture any error > pass > > > > -- > Steven. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list