jps services
  • Home Page
  • GIS Training Courses
    • QGIS Training Courses
    • ArcGIS Training Courses
    • MapInfo Pro Training Courses >
      • MapBasic Training Course
  • About
  • Blog

Using the OnError statement in MapBasic

6/12/2016

0 Comments

 
When you have successfully compiled your code you may still encounter problems when trying to run it in MapInfo Professional. This can stem from a number of reasons including logic errors, mistakes in how files are opened or closed, or syntax errors in an SQL statement for example.

MapBasic has a number of options to avoid code failing because of errors that can occur. One of these options is the OnError statement. Use the MapBasic Help to search for the OnError statement. Help describes this function as follows:  “The OnError statemement either enables an error-handling routine, or disables a previously enabled error-handler. (An error-handler is a group of statements executed in the event of an error).”
​
Below is an example of how the OnError statement could be used which is based on the code sample provided within MapBasic Help.​
Picture
If both the States and City_125 tables are in the same directory as the code sample then both tables will load in MapBasic Pro as in the following image.
Picture
Remove the States table from the code sample directory and run the code again.
​
If the States map is not found by the code then the following message will be displayed in MapBasic Pro.
Picture

If you have run the code you will note that once this message appears the program terminates without loading the City_125 table even if it is available in the
correct directory. This happens because the code jumps to :-

no_states:

Note "Could not open table States... no Map used."
Resume after_mapfrom

At this point the after_mapfrom code informs the program to terminate.

If only  the States table is in a directory which is accessible to the code then the following Note message will appear in MapInfo Pro. Again to run this example first remove the City_125 table from the application directory. ​
Picture
When you click OK on this Note message the program proceeds to the next line of code:-

no_cities:

            Note "City data not available..."
            Map From states
            Resume after_mapfrom

This statement Map From states will instruct MapInfo Pro to load the States table as shown in the following image;-


Picture
When the program loads the States table the code moves to the next line: Resume after_mapfrom and this results in the program ending.​

This code sample is an example of how MapBasic can help you avoid unanticipated errors causing your program to fail. The OnError option is one of several error functions available to help you in coding MapBasic. Use the MapBasic Help feature to also look at the Err() and Err$() functions for further advice with error handling.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Author

    Joe Short BSc has been involved with various mapping solutions for over twenty years.  If you are considering implementing a GIS  or have ArcGIS Pro, MapInfo Pro or QGIS training requirements, jps services would be happy to be of assistance to your organisation. 

    Archives

    April 2020
    March 2020
    October 2019
    September 2019
    August 2019
    July 2019
    March 2019
    November 2018
    October 2018
    August 2018
    July 2018
    November 2017
    October 2017
    September 2017
    July 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    May 2016
    February 2016
    September 2015
    August 2015
    April 2015
    February 2015
    November 2014
    October 2014
    July 2014
    June 2014
    May 2014
    March 2014
    February 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    June 2013
    May 2013
    April 2013
    February 2013
    December 2012
    October 2012
    September 2012
    August 2012
    July 2012

    Categories

    All
    Arc Gis
    Arcview
    Autodesk
    Cad
    Gis Training
    Local Government
    Mapbasic
    Mapinfo
    Quantum Gis
    Relational Databases
    Saga Gis

    RSS Feed