It means catching an Exception and performing some logic based on its type, so that your application can handle it gracefully rather than abruptly shutting down.
Here is an example (albeit a contrived one) in Java:
public int arrayRetrieve(int[] a, int index) {
return a[index];
}
Given this function, there is no guarantee that index
will be a valid position in a
. In Java, this will throw an ArrayOutOfBoundsException
.
Code that calls arrayRetrieve
needs to be aware of this possibility, and handle this case accordingly:
int num = 0;
try {
num = arrayRetrieve(someArray, 77);
} catch (ArrayOutOfBoundsException e) {
// Set num to a default value, or log an error, or however you want to handle this case
}
If the ArrayOutOfBoundsException
were not caught, this would cause the program to crash instead.
(One reason I said this example is contrived is because Java in particular has two kinds of Exception
- the kind you have to catch explicitly, and the kind you don't. ArrayOutOfBoundsException
is an example of the latter.)