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.
If the States map is not found by the code then the following message will be displayed in MapBasic Pro.
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.
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;-
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.