Exception Handling
Exception Handling
HANDLING
6.53
In addition to these classes, Java provides a wide range of predefined exceptions that can
be used to handle specific types of errors. It is also possible to define custom exception
classes to handle program-specific errors.
This code reads the contents of a file named "file.txt". If an IOException occurs (e.g., if
the file does not exist or cannot be read), the catch block will handle the exception.
A run time error occurs when the program is being executed length() method is called
on a null reference str. The length() method is a method of the String class that returns
the length of the string, but since str is null, there is no object on which to call the
method, and the program will crash with a NullPointerException.
This code tries to get the length of a null array, resulting in a NullPointerException
This code tries to access an array element that is outside the bounds of the array, resulting
in an ArrayIndexOutOfBoundsException.