468,469 Members | 2,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,469 developers. It's quick & easy.

Error Trapping

I ran the following program to retrieve entries from the windows
registry on Windows XP:
import win32api, win32con
aReg = win32api.RegConnectRegistry(None,
win32con.HKEY_CURRENT_USER)
aKey = win32api.RegOpenKeyEx(aReg,
r"Software\Microsoft\Internet Explorer\PageSetup")
for i in range(100):
Name, Data, Type = win32api.RegEnumValue(aKey, i)
print "Index=(", i,") Name=[",
Name,"] Data=[",Data,"]
Type=[",Type,"]"
win32api.RegCloseKey(aKey)

This is the program output:

[code:1:8bb6fccb25]Index=( 0 ) Name=[ header ]
Data=[ &w&bPage &p of &P ] Type=[ 1
]
Index=( 1 ) Name=[ footer ] Data=[
&u&b&d ] Type=[ 1 ]
Index=( 2 ) Name=[ margin_bottom ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 3 ) Name=[ margin_left ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 4 ) Name=[ margin_right ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 5 ) Name=[ margin_top ] Data=[ 0.750000
] Type=[ 1 ]

Traceback (most recent call last):
File "F:/temp/Python Test Folder/Read Windows Registry
Entries (No error trapping).py", line 5, in -toplevel-
Name, Data, Type = win32api.RegEnumValue(aKey, i)
error: (259, 'PyRegEnumValue', 'No more data is
available.')[/code:1:8bb6fccb25]

I received an error because I tried to read past the last entry for
this key. The following is a modified version of the program to trap
any error on key entry retrieval:

[code:1:8bb6fccb25]import win32api, win32con, sys
aReg = win32api.RegConnectRegistry(None,
win32con.HKEY_CURRENT_USER)
aKey = win32api.RegOpenKeyEx(aReg,
r"Software\Microsoft\Internet Explorer\PageSetup")
for i in range(100):
try:
Name, Data, Type = win32api.RegEnumValue(aKey, i)
print "Index=(", i,") Name=[",
Name,"] Data=[",Data,"]
Type=[",Type,"]"
except:
break
win32api.RegCloseKey(aKey)[/code:1:8bb6fccb25]

This is the program output:

[code:1:8bb6fccb25]Index=( 0 ) Name=[ header ]
Data=[ &w&bPage &p of &P ] Type=[ 1
]
Index=( 1 ) Name=[ footer ] Data=[
&u&b&d ] Type=[ 1 ]
Index=( 2 ) Name=[ margin_bottom ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 3 ) Name=[ margin_left ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 4 ) Name=[ margin_right ] Data=[ 0.750000
] Type=[ 1 ]
Index=( 5 ) Name=[ margin_top ] Data=[ 0.750000
] Type=[ 1 ][/code:1:8bb6fccb25]

The latter program will trap any error resulting from trying to
retrieve the key values. What I want to do is to specifically trap
the error denoting that there is no more data available so I can
continue executing more code as oppose to aborting the program. if
the error is something other than no more data available, then I want
to abort the program with an error message.

What code is needed to specifically trap the "no more data is
available" error?

Thank you in advance for your help!

Jul 2 '06 #1
0 1268

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Robert Wing | last post: by
2 posts views Thread by Steve Richfield | last post: by
13 posts views Thread by Thelma Lubkin | last post: by
3 posts views Thread by Smriti Dev | last post: by
2 posts views Thread by Bill Stock | last post: by
2 posts views Thread by Captain Nemo | last post: by
9 posts views Thread by 47computers | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.