Handle errors and warnings¶
This article was originally posted to the AIMMS Tech Blog.
This article explains how to add custom warning messages for certain errors.
When you send your AIMMS project into the world, errors will inevitably pop up from time to time. Users may provide the wrong kind of input for your model resulting in AIMMS throwing errors that your user may not understand.
Starting with AIMMS 3.10, we added error handling routines that allow you to not only catch existing AIMMS errors, but also raise your own errors.
Error handling in AIMMS can be done at two levels: * You can set a global error handler procedure for your project, which will be called whenever an error occurs anywhere in your code. * You can set an error handler for a specific subset of your code that is within a block statement in AIMMS (comparable to try-catch blocks in languages like C/Java/Python).
This article focuses on the second approach.
In the example below, let’s calculate the division of two parameters for user input values and display the result:
result := parameter1 / parameter2 ; DialogMessage("Result of division = " + result ) ;
Now let’s assume the user provides the value 0 for
parameter2, and execution of the first statement produces a division-by-zero error in AIMMS. That means the execution is halted, including the second statement.
With the error handling, you can catch this division-by-zero error and tell the user what went wrong.
Error handling functionality in AIMMS is in the scope of the predefined module
ErrorHandling with the prefix
First, introduce a new element parameter with range
errh::PendingErrors. This element parameter can be declared locally for the procedure or globally in any declaration section.
Once that’s done, you can catch errors and provide custom error messages:
block result := parameter1 / parameter2 ; DialogMessage("Result of division = " + result ) ; onerror err do !If any runtime errors occur in the statements executed in the above !block, AIMMS will execute the statements in this error handling block !Tell the user that there is an error in the input DialogMessage("Make sure parameter2 is unequal to 0 to prevent division by 0", "Caught error in input") ; !Now tell AIMMS that we have handled the error. If we skip this statement !AIMMS will consider the error as unhandled, meaning it will go to the next !layer in the list of error handlers. If no error handler marks the error !as handled, the result is the same as without error handling: AIMMS will !display the error in the error window and halt further execution. errh::MarkAsHandled(err , 1) ; !If you want, you can use the other functionality in the errh module to !get more information about the error, for example the message that would !be printed to the error window: DialogMessage("Error message = \n" + errh::Message( err ) ) ; !You can also show the user whether the error that was caught was an !error or just a warning: DialogMessage("Severity = " + errh::Severity( err ) ) ; end block ;
Now your user can easily figure out what’s wrong.
Last Updated: September, 2019