Overview
The current exception handling mechanism of Cava is not mature enough. You cannot run the try or exception command to capture or handle exceptions. Cava automatically throws an exception if it identifies a division by zero, an array-index out of bound, or a null pointer access. If Cava identifies one of the preceding exceptions when it runs code, it stops running the code and returns the exception details. We recommend that you check the validity of a request to divide a value, access an array, or access an object before you send the request. This prevents exceptions.
Division by zero
Sample code:
int b = 0;
int a = 1 / b; // Cava throws an exception because a division by zero is identified.
----------------------------------------------------------------------
double c = 0;
double b = 1 / c; // Cava does not throw an exception if a DOUBLE value is divided by 0.
int a = 1 / c; // Cava does not throw an exception if an INT value is divided by 0.
Best practice:
int b = 0;
int a = 0;
if (b != 0) {
a = 1 / b;
}
Array-index out of bound
Sample code:
int[] a = new int[10];
a[-1] // Cava throws an exception because the specified element to be accessed is below the lower limit.
a[10] // Cava throws an exception because the specified element to be accessed exceeds the upper limit.
Best practice:
int[] a = new int[10];
int idx = 10;
if (idx > 0 && idx < a.length) {
int b = a[idx];
}
Null pointer access
Sample code:
Person student = null; // Person is a pre-defined class.
student.setAge(15); // The object is set to null. You cannot access the object by using an index.
Best practice:
Person student = null;
if (student != null) {
student.setAge(15);
}
Manually throw an exception
Sample code:
Abort.abort();
Abort.abort("exception");