problem using OnError in Mapbasic

Veljean
Veljean used Ask the Experts™
on
Hi every body!  Im using MapBasic  (Mapinfo development language).


i want to catch a error event when in run time the progrma cant find the table indicated in the line:

Open Table "Table1"

i have the following:

For TableCounter=1 to TotalTables

Opent Table "Table"+TableCounter //all my tables have names like table1,table2, etc
do things...

Close All
Next



i tried:

For TableCounter=1 to TotalTables
OnError goto no_Table
Opent Table "Table"+TableCounter
do things...

no_Table:
Close All
Next

But only the first iteration works , the second one show the classic error of table not found, what can i do? I hope that OnError Statement works the same for VB and somebody can help me :(
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
GrahamSkanRetired
Top Expert 2012

Commented:
If it is anything like VB, you need to reset the error in order for error trapping to work.
In VB that generally means using Resume

    For TableCounter=1 to TotalTables
       OnError goto no_Table
NextTable:
       Open Table "Table"+TableCounter
       do things...
    Next
    '...
no_Table:
    Resume NextTable
'...

Open in new window

Author

Commented:
mmmm doesnt work...let me see if i understand... when an error occurs for example in the iteration 3 , the program goes to no_Table and after resume to  NexTable opening again a table? should keep the count of the for statement?
GrahamSkanRetired
Top Expert 2012

Commented:
What does happen?

Actually, I think that it should have gone to the next statement

    For TableCounter=1 to TotalTables
       OnError goto no_Table

       Open Table "Table"+TableCounter
       do things...
NextTable:
    Next
    '...
no_Table:
    Resume NextTable
'...

The point is that if you are in error-handling mode, you cannot trap another error.

Also it you wanted to step through a collection in VB, you would use the 'For Each' construct. That way you only find existing tables/


For Each tbl in MyCollection.Tables
    Open tbl
    'or perhaps
    'Open tbl.Name
Next

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Mapbasic doesn't  have for each, is very rudimentary sorry about that... i applied your code and i got the following error message :

"Found Resume while not processing and error"

I know that VB is very superior to Mapbasic but i think at least Onerror statement should works the same for both.
Retired
Top Expert 2012
Commented:
Has your code dropped through into your error handler? Make sure that you have an Exit Sub before the handler:


Sub MySub()
    For TableCounter = 1 To TotalTables
        On Error GoTo no_Table
    
        Open Table "Table"+TableCounter
    'do things...
NextTable:
    Next
    '...
    Exit Sub '<---
no_Table:
    Resume NextTable
    '...
 
End Sub

Open in new window

Author

Commented:
I have exactly this code:


as you can see i dont have code after "Next".
One question... all the code after the label is gonna be processed although no error is catched?
For TableCounter = 1 To TotalTables
 
OnError GoTo no_Table
Open Table "Table"+TableCounter
	
'...
 
NextTable:
Next
 
no_Table:
 
Resume NextTable
 
End Sub

Open in new window

Author

Commented:
Thank you very much!
GrahamSkanRetired
Top Expert 2012

Commented:
Yes. If all the tables have been processed, your code will carry on into the error handler and try to process a non-existent error.

Commented:
The above solution obviously works.
Keep in mind that there is another approach:
The Mapbasic ERRORS.DOC file, provides error codes. I have often used the specific error code to fine tune error handling.
In this case I think the error code is 418 - "Table not found".
Yu can find the exact error code by adding error functions. This often enables you be more specific and in some cases is a great debug tool.

Laurence

Author

Commented:
Is a pain in the ass debugging with Mapbasic , im starting to use it and still cant find a good way to do it, anyway thanks for the tip Iseeff

Commented:
Yes, it is a little cumbersome.
The quickest way to do it is as follows:
1. Learn the keyboard shortcuts - Crtl+S to save your changes, then, ctrl+k to compile, then ctrl+u to run it in Mapinfo.
2. Add a "close all" statement at the begining of your code to close all the open MI tables.
3. Make sample files of the tables to reduce processing time.
4. Hard code the paths to tables if you have user prompts, once you got it working, revert to the prompts.
5. Use the Mapbasic window in MI.
Open the window, manually do the things you need Mi to do and then you have a decent lot of code to work with...

So, if you Ctrl+S+K+U...you ill run your progam quickly from Mapbasic...

Laurence

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial