I think this is a dumb question but I hear and see the term exception handling a lot. I have used try/catch, but i am still wondering what on earth ‘handling‘ means. Can anyone kindly give some example that we can say the exception is actually ‘handled‘?
sorry for poor English, hope i made myself clear.
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:
Given this function, there is no guarantee that
indexwill be a valid position ina. In Java, this will throw anArrayOutOfBoundsException.Code that calls
arrayRetrieveneeds to be aware of this possibility, and handle this case accordingly:If the
ArrayOutOfBoundsExceptionwere 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.ArrayOutOfBoundsExceptionis an example of the latter.)