Error trapping while reading windows XP registry entries

Reply

Join Date: Jul 2006
Posts: 2
Reputation: JohnJohnUSA is an unknown quantity at this point 
Solved Threads: 0
JohnJohnUSA JohnJohnUSA is offline Offline
Newbie Poster

Error trapping while reading windows XP registry entries

 
0
  #1
Jul 3rd, 2006
I ran the following program to retrieve entries from the windows registry on Windows XP:

  1. import win32api, win32con
  2. aReg = win32api.RegConnectRegistry(None, win32con.HKEY_CURRENT_USER)
  3. aKey = win32api.RegOpenKeyEx(aReg, r"Software\Microsoft\Internet Explorer\PageSetup")
  4. for i in range(100):
  5. 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)
  6. print "Index=(", i,") Name=[", Name,"] Data=[",Data,"] Type=[",Type,"]"
  7. 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:

  1. import win32api, win32con, sys
  2. aReg = win32api.RegConnectRegistry(None, win32con.HKEY_CURRENT_USER)
  3. aKey = win32api.RegOpenKeyEx(aReg, r"Software\Microsoft\Internet Explorer\PageSetup")
  4. for i in range(100):
  5. try:
  6. Name, Data, Type = win32api.RegEnumValue(aKey, i)
  7. print "Index=(", i,") Name=[", Name,"] Data=[",Data,"] Type=[",Type,"]"
  8. except:
  9. break
  10. 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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Error trapping while reading windows XP registry entries

 
0
  #2
Jul 3rd, 2006
Here is an example error:
  1. >>> List = ['item1','item2','item3']
  2. >>> for item in range(10):
  3. print List[item]
  4.  
  5.  
  6. item1
  7. item2
  8. item3
  9.  
  10. Traceback (most recent call last):
  11. File "<pyshell#7>", line 2, in -toplevel-
  12. print List[item]
  13. IndexError: list index out of range
  14. >>>
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:

  1. >>> List = ['item1','item2','item3']
  2. >>> for item in range(10):
  3. try:
  4. print List[item]
  5. except IndexError:
  6. print "There are no more items in the list."
  7.  
  8.  
  9. item1
  10. item2
  11. item3
  12. There are no more items in the list (IndexError).
  13. There are no more items in the list (IndexError).
  14. There are no more items in the list (IndexError).
  15. There are no more items in the list (IndexError).
  16. There are no more items in the list (IndexError).
  17. There are no more items in the list (IndexError).
  18. 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:

  1. >>> for item in range(10):
  2. try:
  3. print List[item]
  4. print List[item]+1
  5. except TypeError:
  6. print "This is a type error"
  7. except IndexError:
  8. print "This is an index error"
  9. except:
  10. print "This is to catch any unknown errors or errors which don't need handling"
  11.  
  12.  
  13. 1
  14. 2
  15. item2
  16. This is a type error
  17. 3
  18. 4
  19. 4
  20. 5
  21. 5
  22. 6
  23. item6
  24. This is a type error
  25. This is an index error
  26. This is an index error
  27. This is an index error
  28. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Error trapping while reading windows XP registry entries

 
0
  #3
Jul 3rd, 2006
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:
  1. import win32api, win32con, sys
  2. aReg = win32api.RegConnectRegistry(None, win32con.HKEY_CURRENT_USER)
  3. aKey = win32api.RegOpenKeyEx(aReg, r"Software\Microsoft\Internet Explorer\PageSetup")
  4. for i in range(100):
  5. try:
  6. Name, Data, Type = win32api.RegEnumValue(aKey, i)
  7. print "Index=(", i,") Name=[", Name,"] Data=[",Data,"] Type=[",Type,"]"
  8. except win32api.error, details: # This part catches win32api's custom error: 'error', then sticks the info that follows into the tuple variable 'details'
  9. if details[0] == 259: # this checks the error type given by win32api the rest is pretty straight forward
  10. print "Error: %s, %s"%(details[1:])
  11. else:
  12. print "Other error"
  13. break
  14. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC