开发者

Handling errors in Python scripts

开发者 https://www.devze.com 2023-01-18 08:18 出处:网络
Using pyblog.py, I got the following error, which I then tried to more gracefully handle: Traceback (most recent call last):

Using pyblog.py, I got the following error, which I then tried to more gracefully handle:

Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster.py", line 11, in <module>
    date = blogurl.get_recent_posts(1)[0]['dateCreated']
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\pyblog.py", lin开发者_如何学Pythone 129, in get_recent_posts
    return self.execute('metaWeblog.getRecentPosts', blogid, self.username, self.password, numposts)
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\pyblog.py", line 93, in execute
    raise BlogError(fault.faultString)
BlogError: XML-RPC services are disabled on this blog.  An admin user can enable them at http://example.com/blogname/wp-admin/options-writing.php
>>> 

So I tried the following code to without crashing the script:

for blog in bloglist:
    try:
        blogurl = pyblog.WordPress('http://example.com' + blog + 'xmlrpc.php', 'admin', 'laxbro24')
        date = blogurl.get_recent_posts(1)[0]['dateCreated']
        print blog + ', ' + str(date.timetuple().tm_mon) + '/' + str(date.timetuple().tm_mday) + '/' + str(date.timetuple().tm_year)
    except BlogError:
        print "Oops! The blog at " + blogurl + " is not configured properly."

Only to get the following error:

Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster.py", line 13, in <module>
    except BlogError:
NameError: name 'BlogError' is not defined

Isn't the name blog error defined by PyBlog, since that's where I got that name in the first place? Am I using "except" wrong? Thanks for any tips!


Yes, it is using BlogError, but you have not imported BlogError into your namespace to reference. You instead want to be using pyblog.BlogError:

for blog in bloglist:
    try:
        blogurl = pyblog.WordPress('http://example.com' + blog + 'xmlrpc.php', 'admin', 'laxbro24')
        date = blogurl.get_recent_posts(1)[0]['dateCreated']
        print blog + ', ' + str(date.timetuple().tm_mon) + '/' + str(date.timetuple().tm_mday) + '/' + str(date.timetuple().tm_year)
    except pyblog.BlogError:
        print "Oops! The blog at " + blogurl + " is not configured properly."

Remember that exceptions follow the same rules of scoping that any python object does.


Your except is syntactically correct. And yet it fails because you haven't explicitly imported the BlogError exception class into your program's namespace.

To fix this explicitly import the BlogError class. For e.g.

from pyblog import BlogError
try:
    ...
except BlogError:
    ...


the code will be

 from pyblog import BlogError
0

精彩评论

暂无评论...
验证码 换一张
取 消