Jump to content

How to raise exceptions properly...


Recommended Posts

Hi. Here is a piece of code to create a local user (Python). My goal was to check the result of every AutoItX method and exit immediately after first failure. It works but my question is the following:

Is it possible to simplify this code in case I don't care which method was failed? I mean, is it possible not to use "if-raise" constructions with every method to exit correctly after first error or failure?

It doesn't matter what language I use (Jscript, Python, whatever). I'm eager to understand how to do it properly...

import sys
import win32com.client

oAutoIt=win32com.client.Dispatch('AutoItX3.Control')
mscpath = "c:\\windows\\system32\\"
treeitem = "Computer Management (Local)|System Tools|Local Users and Groups"
username = "testuser"

try:
    oAutoIt.Run('cmd /c "compmgmt.msc"', mscpath, oAutoIt.SW_HIDE)
    
    rc = oAutoIt.WinWaitActive("Computer Management", "", 5)
    
    if rc == 0:
        raise UserWarning, 'AutoIt Error: WinWaitActive'
    
    oAutoIt.ControlTreeView ("Computer Management", "", 12785, "Expand", treeitem, "")
    
    if oAutoIt.error == 1:
        raise UserWarning, 'AutoIt Error: TreeView - Expand'

    treeitem = treeitem + "|Users"
    
    oAutoIt.ControlTreeView ("Computer Management", "", 12785, "Select", treeitem, "")

    if oAutoIt.error == 1:
        raise UserWarning, 'AutoIt Error: TreeView - Select'

    oAutoIt.Send ("{AppsKey}{Down}{Enter}")

    rc = oAutoIt.ControlSend("New User", "", "Edit1", username)

    if rc == 0:
        raise UserWarning, 'AutoIt Error: EditBox - UserName'
    
    rc = oAutoIt.ControlClick("New User","", 1170)

    if rc == 0:
        raise UserWarning, 'AutoIt Error: Create Button'
    
    rc = oAutoIt.ControlClick("New User","", 2)
    
    if rc == 0:
        raise UserWarning, 'AutoIt Error: Close Button'

except UserWarning: 
    raise

Thanks in advance, Yuri

Link to comment
Share on other sites

My guess is that you have to check for errors after every AutoIt call.

You could create an AutoIt wrapper that could raise errors/exceptions. It might be dirty but once it's done, it's beautiful in the main program code :)

Edited by amokoura
Link to comment
Share on other sites

My guess is that you have to check for errors after every AutoIt call.

You could create an AutoIt wrapper that could raise errors/exceptions. It might be dirty but once it's done, it's beautiful in the main program code :)

Do you mean instead of this piece of code...

if rc == 0:
        raise UserWarning, 'AutoIt Error: EditBox - UserName'

... write something like that?

AutoItCheckResult (rc, 'EditBox - UserName')

where AutoItCheckResult funciton is defined as

def AutoItCheckResult (rc, Desc)

    if rc == 0:
               Desc = 'AutoIt Error: ' + Desc
               raise UserWarning, Desc

Actually, I don't think it is a good idea. Or did you mean something else?

Thanks in advance, Yuri.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...