Kermeta provides also an exception mechanism. You can define a "rescue" block to manage errors occurring during the execution of another block. Exception mechanism is very close to the Java Exception mechanism.
Example 1 : a simple exception raising
do var excep : kermeta::exceptions::Exception excep := kermeta::exceptions::Exception.new stdio.writeln("Throwing an exception ! ") raise excep end
Any block can then rescue exceptions.
Example 2 : rescue block
var v1 : Integer init 2 var v2 : Integer init 3 do var v3 : Integer v3 := v1 + v2 rescue (myConstraintError : kermeta::exceptions::ConstraintViolatedInv) // something with myConstraintError // ... rescue (myError : kermeta::exceptions::Exception) // something with myError // ... end
Tip | |
---|---|
do not hesitate to create "do .. end" block into another block if you want to check for an exception only in a part of the code. This also works if you want to rescue code from within a rescue code. |