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)

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:

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:

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!

Recommended Answers

All 2 Replies

Here is an example error:

>>> List = ['item1','item2','item3']
>>> for item in range(10):
	print List[item]

	
item1
item2
item3

Traceback (most recent call last):
  File "<pyshell#7>", line 2, in -toplevel-
    print List[item]
IndexError: list index out of range
>>>

if you look at the traceback bit it tells you some useful information, including the error type which in this case is: IndexError

Now all you have to do is include the error type with the except function:

>>> List = ['item1','item2','item3']
>>> for item in range(10):
	try:
		print List[item]
	except IndexError:
		print "There are no more items in the list."

		
item1
item2
item3
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).
There are no more items in the list (IndexError).

as you can see, the new part of the except command, catches the IndexError and does something about it other than killing the program.

you can have more than one except command too, for example if you wanted to catch a few different types of errors:

>>> for item in range(10):
	try:
		print List[item]
		print List[item]+1
	except TypeError:
		print "This is a type error"
	except IndexError:
		print "This is an index error"
	except:
		print "This is to catch any unknown errors or errors which don't need handling"

		
1
2
item2
This is a type error
3
4
4
5
5
6
item6
This is a type error
This is an index error
This is an index error
This is an index error
This is an index error

In your case you need to have 'error' after the except followed by the details within the error, which is the section in brackets: except: error (259, 'PyRegEnumValue', 'No more data is available.')

Just shout if you need anymore help,
a1eio

P.S I'm not on my normal computer so i can't test the above theory with win32api if it doesn't work, just reply with the reason why and i'll fix it. in the meantime i'm trying to get win32api but www.python.net doesn't seem to be responding at the moment.

Ok,
I got my hands on win32api, and i've had a little play with your code, and it turns out what i said:

In your case you need to have 'error' after the except followed by the details within the error, which is the section in brackets: except: error (259, 'PyRegEnumValue', 'No more data is available.')

was completely wrong, but here is a solution i've come up with:

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 win32api.error, details: # This part catches win32api's custom error: 'error', then sticks the info that follows into the tuple variable 'details'
        if details[0] == 259: # this checks the error type given by win32api the rest is pretty straight forward
            print "Error: %s, %s"%(details[1:])
        else:
            print "Other error"
        break
win32api.RegCloseKey(aKey)

The 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.75000 ] Type=[ 1 ]
Index=( 3 ) Name=[ margin_left ] Data=[ 0.75000 ] Type=[ 1 ]
Index=( 4 ) Name=[ margin_right ] Data=[ 0.75000 ] Type=[ 1 ]
Index=( 5 ) Name=[ margin_top ] Data=[ 0.75000 ] Type=[ 1 ]
Error: PyRegEnumValue, No more data is available.

win32api appears to use it's own error class, with it's own error types which is in the brackets: error: (259, 'PyRegEnumValue', 'No more data is available.')

the information after ANY error can be caught by attaching a variable to the end of an except command, after a comma eg:
except: ErrorType, VariableName:

then you can handle the contents of the variable as if it were any other, eg:
print VariableName

in the example above, i've checked the variable 'details' for the error type '259' which is win32api's way of saying no more data is available.

any problems, just say.

a1eio

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.