Sign in to follow this  
Followers 0
deef99

Where to add Error Handling please

13 posts in this topic

I have this script below which works fine most of the time. However, at some point a get an error like:

Autoscipt Error -1 Subscript Out of Range for an Array

or something like that. Of course, the program terminates. I don't know what is causing the error, and I need the program to continue running. Can someone take a look at my script and help me determine where the error might be occuring and/or add some error handling to keep the program alive.

Any help would be appreciated...I can't keep babysitting this process!

-------------------------------------------------------------------------------

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Outfile=..\IBOnly.exe

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include<File.au3>

#include "array.au3"

#include "Date.au3"

#include "String.au3"

While 5 = 5

; Change DIR and find a file

FileChangeDir("\\ardfiles01\common\CMS_IBAuto\IBonly_Create")

$search = FileFindFirstFile("*.acsup")

While 1

;empty all variables

$array = ""

$file = ""

$s0 = "";skill 400 level

$s1 = "";skill 401 level

$s3 = "";skill 403 level

$c666 = "";SSS Cancel save

$c662 = "";SSS Cust Svc

$agt = "" ;701 or 702 ard or cov agent

$al = 5

$agent = ""

$newib = ""

;look for additional files

$file = FileFindNextFile($search)

If @error Then ExitLoop

;check if Universal agent and don't create IB Script

If StringLeft($file, 2) = "U_" Then

FileDelete("U_*.acsup")

ExitLoop

EndIf

;reset the basics

$read = _FileReadToArray($file, $array)

$al = 5 ;start at line 5 where the skills begin

$fline = 1

$CountLines = _FileCountLines($file)

;setup new file name

;$ibname = StringMid($file,5,50)

$agent = StringRight($file, 13)

$agent = StringLeft($agent, 7)

$newib = "\\ardfiles01\common\CMS_IBAuto\Inbound\" & $file

$filenew = FileOpen($newib, 2)

While $al <> $CountLines

If StringRight($array[$al], 3) = "400" Then

$al = $al + 1

$s0 = StringRight($array[$al], 1)

Else

If StringRight($array[$al], 3) = "401" Then

$al = $al + 1

$s1 = StringRight($array[$al], 1)

Else

If StringRight($array[$al], 3) = "403" Then

$al = $al + 1

$s3 = StringRight($array[$al], 1)

Else

If StringRight($array[$al], 3) = "666" Then

$al = $al + 1

$c666 = StringRight($array[$al], 1)

Else

If StringRight($array[$al], 3) = "662" Then

$al = $al + 1

$c662 = StringRight($array[$al], 1)

Else

If StringRight($array[$al], 3) = "701" Then

$agt = StringRight($array[$al], 3)

Else

If StringRight($array[$al], 3) = "702" Then

$agt = StringRight($array[$al], 3)

EndIf

EndIf

EndIf

EndIf

EndIf

EndIf

EndIf

$al = $al + 1

WEnd

; Write data out to a file

FileWriteLine($newib, "Public Sub Main()")

FileWriteLine($newib, "On Error Resume Next")

FileWriteLine($newib, "set AgMngObj = cvsSrv.AgentMgmt")

FileWriteLine($newib, "ReDim SetArr (0,3)")

If $s0 <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 400")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= " & $s0)

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

If $s1 <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 401")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= " & $s1)

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

If $s3 <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 403")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= " & $s3)

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

If $c666 <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 666")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= " & $c666)

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

If $c662 <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 662")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= " & $c662)

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

If $agt <> "" Then

FileWriteLine($newib, "SetArr(" & $fline & ",1)= " & $agt)

FileWriteLine($newib, "SetArr(" & $fline & ",2)= 1")

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

EndIf

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 703")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= 1")

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

$fline = $fline + 1

FileWriteLine($newib, "SetArr(" & $fline & ",1)= 950")

FileWriteLine($newib, "SetArr(" & $fline & ",2)= 1")

FileWriteLine($newib, "SetArr(" & $fline & ",3)= 0")

FileWriteLine($newib, 'AgMngObj.AcdStartUp -1,"", cvsSrv.ServerKey, -1')

FileWriteLine($newib, "AgMngObj.OleAgentSetSkill 1," & $agent & ",1, 0,0, 1," & $fline & ',SetArr,""')

FileWriteLine($newib, "End Sub")

_FileWriteToLine($newib, 4, "ReDim SetArr (" & $fline & ",3)", 1)

FileClose($file)

FileClose($filenew)

FileClose($newib)

FileClose($search)

FileDelete("\\ardfiles01\common\CMS_IBAuto\IBonly_Create\" & $file)

WEnd

;While FileExists("\\ardfiles01\common\CMS_IBAuto\IBonly_Create\on.txt")

; MsgBox(0, "", "Waiting for initial Inbound scripts to complete", 3)

;Sleep(5000)

;WEnd

Sleep(2000)

WEnd

Share this post


Link to post
Share on other sites



deef99,

From a quick look, you are only creating an array at one point:

$read = _FileReadToArray($file, $array)

And then accessing the elements in your While $al <> $CountLines loop.

If the array is not created for some reason, any attempt to access the elements will produce the error you received. So some errorchecking at this point would probably be a good idea. :D

And you might also like to look at using a Switch structure rather than that ugly multiple If thing you wrote!

M23

P.S. When you post code please use Code tags. Put [autoit ] before and [/autoit ] after your posted code (but omit the trailing space - it is only there so the tags display here). Or press the blue button just under the BOLD toolbar button.


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you so much for your fast response!

Can you suggest where and what I should add for the error handling? Sorry for the newbie nonsense... and yes, I will look at switch once I have this code more stable.

Share this post


Link to post
Share on other sites

deef99,

My attempt on adding error-checking of the array and introducing a Switch structure - I hope you can see where it fits:

;reset the basics
        _FileReadToArray($file, $array)
        
        If IsArray($array) Then

            ;setup new file name
            ;$ibname = StringMid($file,5,50)
            $agent = StringRight($file, 13)
            $agent = StringLeft($agent, 7)
            $newib = "\\ardfiles01\common\CMS_IBAuto\Inbound\" & $file
            $filenew = FileOpen($newib, 2)

            For $al = 5 To $array[0] ;start at line 5 (where the skills begin) To the number of lines read in
                
                Switch StringRight($array[$al], 3)
                    Case "400"
                        $s0 = StringRight($array[$al], 1)
                    Case "401"
                        $s1 = StringRight($array[$al], 1)
                    Case "403"
                        $s3 = StringRight($array[$al], 1)
                    Case "666"
                        $c666 = StringRight($array[$al], 1)
                    Case "662"
                        $c662 = StringRight($array[$al], 1)
                    Case "701"
                        $agt = StringRight($array[$al], 3)
                    Case "702"
                        StringRight($array[$al], 3)
                EndSwitch
            Next
                
            ; Write data out to a file
            ; Lots of code omitted here for clarity
            
            
        EndIf
        
    WEnd

What we are doing here is checking $array after the file is read in and only proceeding to read and rewrite the file if it has been created. As another check, we are running the "reading data" loop from 5 to the number of lines read into the array (set automatically in $array[0]) so if there are less then the 5 lines necessary, we will not try and read them - which prevents another "Subscript Out of Range" error.

You can also see how a Switch structure might look - a bit clearer I hope you will admit! :D

I hope this helps - you know where we are if not!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

WOW!!! What a difference in appearance! It sure does look... alot healthier! Let me attempt to put this into production and will let you know results!

THANK YOU SO VERY MUCH!

deef99,

My attempt on adding error-checking of the array and introducing a Switch structure - I hope you can see where it fits:

;reset the basics
        _FileReadToArray($file, $array)
        
        If IsArray($array) Then

            ;setup new file name
            ;$ibname = StringMid($file,5,50)
            $agent = StringRight($file, 13)
            $agent = StringLeft($agent, 7)
            $newib = "\\ardfiles01\common\CMS_IBAuto\Inbound\" & $file
            $filenew = FileOpen($newib, 2)

            For $al = 5 To $array[0] ;start at line 5 (where the skills begin) To the number of lines read in
                
                Switch StringRight($array[$al], 3)
                    Case "400"
                        $s0 = StringRight($array[$al], 1)
                    Case "401"
                        $s1 = StringRight($array[$al], 1)
                    Case "403"
                        $s3 = StringRight($array[$al], 1)
                    Case "666"
                        $c666 = StringRight($array[$al], 1)
                    Case "662"
                        $c662 = StringRight($array[$al], 1)
                    Case "701"
                        $agt = StringRight($array[$al], 3)
                    Case "702"
                        StringRight($array[$al], 3)
                EndSwitch
            Next
                
            ; Write data out to a file
            ; Lots of code omitted here for clarity
            
            
        EndIf
        
    WEnd

What we are doing here is checking $array after the file is read in and only proceeding to read and rewrite the file if it has been created. As another check, we are running the "reading data" loop from 5 to the number of lines read into the array (set automatically in $array[0]) so if there are less then the 5 lines necessary, we will not try and read them - which prevents another "Subscript Out of Range" error.

You can also see how a Switch structure might look - a bit clearer I hope you will admit! :D

I hope this helps - you know where we are if not!

M23

Share this post


Link to post
Share on other sites

deef99,

A small change to the Switch structure - the copy-paste went wrong: :D

Case "702"
    StringRight($array[$al], 3)
; Should of course read
Case "702"
    $agt = StringRight($array[$al], 3)

M23

P.S. When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. :huggles:


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I now get this error:

G:\CMS_IBAuto\VB Source\Test.au3 (49) : ==> Subscript used with non-Array variable.:

For $al = 5 To $array[0]

For $al = 5 To $array^ ERROR

Any suggestions?

Share this post


Link to post
Share on other sites

I had a problem similar to this the other day and i changed my array value to the maximum expected number the array would reach.

Change This:

For $al = 5 To $array[0]

to this:

For $al = 5 To $array[5]

or something else like this:

For $al = 5 To $array[10]

Im still very new to programming, so hope this helps

Share this post


Link to post
Share on other sites

I had a problem similar to this the other day and i changed my array value to the maximum expected number the array would reach.

Change This:

For $al = 5 To $array[0]

to this:

For $al = 5 To $array[5]

or something else like this:

For $al = 5 To $array[10]

Im still very new to programming, so hope this helps

This suggestion is based on what?

My guess would be that $array isn't an array looking at the error so check how it should be initialized.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Fixed it... thanks!

Share this post


Link to post
Share on other sites

deef99,

The code I suggested should have prevented that. Did you include the IsArray line?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I had put it in the wrong place...moved it after the _FileReadtoArray and it's happy.

Share this post


Link to post
Share on other sites

deef99,

Position always matters - or so I am told..... :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0