0

I'm working on some MapBasic code and in the process I want it to open the .tab files in a given folder. But when it reaches a .tab file that it is not able to open (e.g. attached to a raster that does not exist, ID/DAT/MAP files missing, etc.), I get the following warning message and it won't open the files that come after it:

enter image description here

Snppet of code:

If FileNames(i) like "%TAB" then
    Open Table FileNames(i)
End If

Is there away to have the code continue after this type of error is encountered so that it will load all of the non-corrupted files? Or better yet have the code load all the files and give a list/report at the end of which files it was unable to open?

2

There are few ways to accomplish this. One way is to create a function for opening tables where you would process whole operation. This is just a fast example:

Function OpenTab(ByVal TableName as String, ByVal sAlias as String) As Logical

    OnError Goto NotOpen  

    Open Table TableName+".TAB" As sAlias Interactive
    Print "Requested table:" + sAlias
    OpenTab = TRUE
    Exit Function

    NotOpen:
    OpenTab = FALSE
    print "---------------------------------------"
    print "Failed to open table "+TableName
    print "---------------------------------------"

End Function
2

Yes, MapBasic provides some error handling functions. In this case you would use the OnError statement to enable an error handling routine which could, for example, log the error in a text file or just resume at the next line of code. Have a read in the MapBasic reference guide (in the documentation folder of your MapInfo installation) of the OnError and Resume statements particularly.

e.g.

Declare Sub Main

Sub Main()

    '.... do some stuff

    OnError GoTo CaughtEx  '// turn on error handling
    If FileNames(i) like "%TAB" then
        Open Table FileNames(i)
    End If
    OnError GoTo 0 '// turn off error handling

Exit Sub

CaughtEx:  '// error handling routine
    '// Log error here...
    Print "Failed to open table " & FileNames(i)
    Resume Next  '// resume code at line after the one which threw the error
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.