An error is a special kind of variable that is used to declare that something has gone wrong in the system. Modulo1 will declare an error automatically, if its rules have been violated. But it is also possible for you to write code that declares custom errors if your own rules have been violated by some circumstance within the application's current state.
For example, it might be a rule that a particular variable is only allowed to contain a value between
100. To enforce this rule, you could include the following within your app:
1 2 3 4 5 6
We start by using an if statement to evaluate the conditions that might trigger an error. And in the event that our if statement returns true (which would subsequently allow its code block to be run), the error that is sitting inside the if statement will be declared, which might potentially cause the code to shut down.
By default, all errors can cause the code to shut down immediately. But it is possible to write code in such a way that it can recover from many kinds of errors and avoids shutting down.
This is achieved by writing your code within a specialised kind of code block called a Try/Catch block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
When Modulo1 encounters the
try section, it will run normally until the
error is encountered. In this example, that will occur if the
result1 variable is not between
100. If that does not occur, it is quite possible that no
errors will be declared in this function, and therefore Modulo1 will ignore the
But if the
error is declared, however, the
try section will stop running, and then Modulo1 will run the
catch section. Modulo1 will pass the
error along to the
catch section so that the
error's information is available. In this case, the
catch block has chosen to refer to the error by the name
err, but you can choose to use any name you like.
catch section is intended to contain whatever code is neccessary for your code to recover from the problem. In many cases you don't need to do anything at all to recover, and it might be enough to simply print our the error message, along with any variables that were stored within the error.
To create an error without any message or data:
To create an error with a message:
Spaces and line breaks are not important. The following achieves the exact same result:
1 2 3
To create an error with a message and some additional data:
1 2 3 4
You can attach almost any kind of variable to an error. Here is another example:
1 2 3 4 5
A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.
The message property can be used to retrieve the message that was included in the error declaration. If no message was included, then this will return
The type property can be used to find out the data-type of a particular error variable. The result will always be
There are no functions specific to error variables