Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify and trapping specific error reading WinXPregistry values

Status
Not open for further replies.

JohnJohnUSA

Programmer
Jul 2, 2006
3
US
I ran the following program to retrieve entries from the windows registry on Windows XP:

Program Code:

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)

Program output:

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.')


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:

Program Code:

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)

Program output:

Code:
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 ]

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!
 
Try this:

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:
cla, exc, trbk = sys.exc_info()
if exc.__dict__["args"][2] == "No more data is available.":
print "No more keys"
break
else:
print "Some other error"
break
win32api.RegCloseKey(aKey)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top