| | |
Error trapping while reading windows XP registry entries
![]() |
•
•
Join Date: Jul 2006
Posts: 2
Reputation:
Solved Threads: 0
I ran the following program to retrieve entries from the windows registry on Windows XP:
Program output:
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 output:
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!
Python Syntax (Toggle Plain Text)
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, <a rel="nofollow" class="t" href="http://www.tek-tips.com/viewthread.cfm?qid=1249756&page=1#" target="_blank">Data</a>, Type = win32api.RegEnumValue(aKey, i) print "Index=(", i,") Name=[", Name,"] Data=[",Data,"] Type=[",Type,"]" win32api.RegCloseKey(aKey)
•
•
•
•
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.')
Python Syntax (Toggle Plain Text)
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)
•
•
•
•
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 ]
What code is needed to specifically trap the "no more data is available" error?
Thank you in advance for your help!
Here is an example error:
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:
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:
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.
Python Syntax (Toggle Plain Text)
>>> 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 >>>
Now all you have to do is include the error type with the except function:
Python Syntax (Toggle Plain Text)
>>> 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).
you can have more than one except command too, for example if you wanted to catch a few different types of errors:
Python Syntax (Toggle Plain Text)
>>> 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.
Last edited by a1eio; Jul 3rd, 2006 at 5:35 am.
Ok,
I got my hands on win32api, and i've had a little play with your code, and it turns out what i said:
was completely wrong, but here is a solution i've come up with:
The output:
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
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.')
Python Syntax (Toggle Plain Text)
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)
•
•
•
•
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.
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
![]() |
Similar Threads
- Editing windows registry (VB.NET)
- I don't think anything too serious but it says every time I restart my computer it (Windows NT / 2000 / XP)
- POSSIBLE windows registry error - urgent attention (Windows NT / 2000 / XP)
- Moving Programs/Applications to Slave Drive (Windows NT / 2000 / XP)
- WinXP Reinstallation (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: File Modification Time
- Next Thread: How to set default directory for saving files in Python IDLE
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation array assignment backend beginner binary bluetooth builtin calculator character cmd converter countpasswordentry curved customdialog cx-freeze dan08 data decimals dictionary directory exe file float format function generator getvalue gnu halp heads homework http ideas input ip itunes java leftmouse line linux list lists loop maintain maze millimeter module mouse mysqlquery number numbers output parsing path pointer prime programming push py2exe pygame python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver terminal text thread time tlapse tuple twoup ubuntu unicode urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib





