Error Handling Tutorial
Luban use a special type: error type to handle errors. The value of error type is generated as error happens in Luban code evaluation. The basic principle of Luban error handling is very simple and can be described in below two points:
· Propagate error value along the line of expression, assignment and control flow if sensible.
· Abort the evaluation of Luban structure or script if the error can not be propagated in a sensible way.
There are enough “who catch what at where” confusion about exception in many different languages. Luban goes a different way, it does not have exception at all, neither try and catch statements. The basic idea is that to use error value to represent error is simpler and easier to understand. Error value also fits Luban’s data flow model very well.
// below variables all contain error
firsterr =
1/0;
alsoerr1 = firsterr++;
alsoerr2 = firsterr
+ alsoerr1;
test = firsterr
isa error and alsoerr1 isa
error and alsoerr2 isa error; // test = true
In above script, first error is generated by operation to divide with zero. Then the error propagates through a series of expression and assignment statements. So the error happens and spread logically corresponding to the code structure. Responsible user can decide to check error and take action at any point.
There are certain points that is error sensitive in Luban. When errors happen at those points, Luban can only stop the evaluation of the current structure or script and report error. These points are listed as following.
Failure of assignment is one of the error sensitive points. Failure of assignment can be caused by assign object of wrong type to typed variable or structure property, for example:
int intvar;
intvar =
3.1416; // world ends here, assign double to a int type variable
std::println(obj=”Devil shall never see the light”); // never reach this
line
If the result of condition expression inside control flow statement is not of bool type, Luban will stop the evaluation because it can not determine the control flow.
X = 0;
if ( X ) // X is not bool
type, evaluation stop
X++;
When the call to a process structure stops because of error, the call returns error value instead of a fully evaluated structure.
Except the above error sensitive points, when error happens Luban will happily continue and pass the error object around. It is totally up to the user to decide where they want to check the error or if they want to check the error at all.