Is there a way i can set a try and catch ArrayIndexOutOfBound exception to check if the array size is greater than 3? For example if the parameter array args[] holds more than 3 values, then i want the exception to show an error.
Here is my code so far:
public static void main (String[] args){
try {
args[4] = "four";
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds.");
}
}
You can use args.length to get the number of elements in the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With