Cava's exception handling is limited — try and exception are not supported. Cava automatically throws an exception and stops execution when it encounters a division by zero, an array index out of bounds, or a null pointer access. Validate inputs before each of these operations to prevent runtime exceptions.
To manually stop execution and signal an error, use Abort.abort().
Division by zero
Cava throws an exception only when an int value is divided by zero. Dividing a double value by zero does not throw an exception.
Guidelines:
Always check that a divisor is non-zero before dividing
intvalues.
Code that throws an exception:
int b = 0;
int a = 1 / b; // Throws an exception: int division by zeroCode that does not throw an exception:
double c = 0;
double b = 1 / c; // No exception
int a = 1 / c; // No exceptionSafe pattern:
int b = 0;
int a = 0;
if (b != 0) { // Guard against zero before dividing
a = 1 / b;
}Array index out of bounds
Cava throws an exception when you access an array element at a negative index or at an index equal to or greater than the array's length.
Guidelines:
Check that the index is greater than
0(lower limit).Check that the index is less than
a.length(upper limit).
Code that throws an exception:
int[] a = new int[10];
a[-1]; // Throws an exception: index below lower limit (0)
a[10]; // Throws an exception: index exceeds upper limit (a.length - 1)Safe pattern:
int[] a = new int[10];
int idx = 10;
if (idx > 0 && idx < a.length) { // Validate both bounds before access
int b = a[idx];
}Null pointer access
Cava throws an exception when you call a method or access a property on a null object.
Guidelines:
Check that an object is not null before calling any method on it.
Code that throws an exception:
Person student = null; // Person is a pre-defined class
student.setAge(15); // Throws an exception: object is nullSafe pattern:
Person student = null;
if (student != null) { // Verify the object is initialized before use
student.setAge(15);
}Manually throw an exception
Use Abort.abort() to stop execution immediately and signal an error from within your Cava script. Pass a message string to include error details.
Abort.abort(); // Stop execution with no message
Abort.abort("exception"); // Stop execution with an error message