简体   繁体   中英

Catch EOL exception in python

I m trying to execute some code statement dynamically in Python using exec()

The code statement is accepted from the user input.

How do I handle the EOF exception that occurs when the string quotes are not closed?

For eg.

 >> exec('if 5 > 1: print "5 is greater)
    # of course, this will raise an exception, 
    # how do I handle this 

How do I handle such cases, using a try-catch ? What exception should I be looking for here?

EOFError is what you are looking for

This link should help you http://docs.python.org/2/library/exceptions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM