Session 11 |
Introduction
In Java, any program is analysed before its execution. Program compilations allow to detect and fix most of the errors; however, some errors occur when the program is running. Some examples:
int[] myNumbers = { 1, 2, 3 }; System.out.println(myNumbers[42]); // ArrayIndexOutOfBoundsException int n = Integer.parseInt("text"); // NumberFormatException String myString = null; myString.length(); // NullPointerException
Runtime errors occur if the JVM detects an operation that is impossible to carry out while a program is running (e.g. try to access an array using an index that is out of bounds - ArrayIndexOutOfBoundsException
, use a double value when your program expects an integer - InputMismatchException
).
An exception is an object that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate abnormally. In other words, an exception is a run-time error. How to handle the exception so that the program can continue to run or else terminate gracefully?
Session 11 |