Try...Catch...Finally

Learn to handle runtime errors and ensure resource cleanup using try, catch, and finally blocks.

Program errors are unavoidable. They can happen because our application is trying to reach a server that is not responding, because we run out of computer memory, or because our program is trying to read a non-existent file. In any case, it is likely that an exception occurs.

Exceptions are program states where the application can no longer continue to run normally, so we have to address them.

Consider the following example. We create an array of size four, but we attempt to access the fifth element (which does not exist). This produces an exception.