15
Exception Handling
During the execution of a program, unexpected events can occur. For instance:
• The program may try to access an index in an array greater than the length of an array
• The program may attempt to invoke a method on a null reference
• The program may attempt to open a file that does not exist
When these events occur an exception is generated, and either:
• Some other code in the program will handle the exception, allowing the program to recover, or
• The program will halt
Exception handling is one of the most important subjects to understand with the Java language, and defining effective approaches to exception handling is one of the key ways to build stable and robust software.