Error Trapping

Discussion in 'Python' started by JohnJohnUSA, Jul 2, 2006.

  1. JohnJohnUSA

    JohnJohnUSA Guest

    I ran the following program to retrieve entries from the window
    registry on Windows XP

    import win32api, win32co
    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=[

    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

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

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

    [code:1:8bb6fccb25]import win32api, win32con, sy
    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
    brea
    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=[

    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 ][/code:1:8bb6fccb25

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

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

    Thank you in advance for your help
     
    JohnJohnUSA, Jul 2, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. toylet

    trapping file i/o error

    toylet, Feb 19, 2004, in forum: Perl
    Replies:
    15
    Views:
    896
    Tassilo v. Parseval
    Feb 23, 2004
  2. Brett Robichaud

    Exceeding File Upload max size - trapping error

    Brett Robichaud, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,902
    bruce barker
    Apr 7, 2004
  3. Craig Wilson

    Trapping the 404 (resource not found) error

    Craig Wilson, Oct 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    478
    =?Utf-8?B?TGVvbiBXZWxpY2tp?=
    Oct 8, 2004
  4. VB Programmer

    Best standard for error trapping?

    VB Programmer, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    396
    VB Programmer
    Oct 25, 2004
  5. =?Utf-8?B?UGF1bA==?=

    Error Trapping and Error Line Number

    =?Utf-8?B?UGF1bA==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    487
    Hans Kesting
    Oct 27, 2005
Loading...

Share This Page