Java Exception Descriptions
Exceptions in Java are thrown for various reasons. In general, an exception is caused by something that is not part of the normal execution of a program. Having an understanding of the different types of exception in Java helps when you attempt to handle them. A few exceptions comprise the most common varieties that beginners are likely to encounter, and learning to cope with these equips you with the skills for exception-handling generally.
-
Class Cast Exceptions
-
Class Cast Exceptions are thrown when a program tries to cast objects inappropriately. In Java, you can cast an object from one class to another as long as the two classes exist within the same hierarchy, i.e., where the new type is either a subclass or superclass of the object's existing class. If you try to perform a casting operation that is not valid because the two classes concerned are not within the same hierarchy, a Class Cast Exception will be thrown.
Number Format Exceptions
-
Among the most common types of exception extending the Illegal Argument Exception class, Number Format Exceptions occur when Java code tries to convert a text string to a particular number type but the string does not contain appropriate characters for the number type in question. Converting from a string to a number is a common task in Java, particularly in cases where the string has been received as a parameter, for example a command line argument. Converting a string to a number type will cause a Number Format Exception if the string does not contain a number in the correct format.
-
Input Output Exceptions
-
Input Output Exceptions, or "IOExceptions" in Java are caused when something goes wrong when reading from or writing to some external resource. There are many types of I/O exceptions in Java, but the type IOException is the main superclass for all exceptions caused by these operations. An example of an IOException would be one caused by attempting to write to or read from a file that does not exist.
Null Pointer Exceptions
-
Java Null Pointer Exceptions are thrown when some part of program execution attempts to access an object but instead encounters "null." Java object references point to the location in memory where the data for the object is stored, but if the object does not exist, for example because it has not been instantiated, the reference will instead point to null. If the program attempts to carry out an operation on a null object, such as calling a method on it or reading one of its fields, a Null Pointer Exception will be thrown.
Index Out Of Bounds Exceptions
-
Index Out Of Bounds Exceptions are commonly encountered when using strings and arrays, in which case they typically take the form of subclasses, Array Index Out Of Bounds and String Index Out Of Bounds. If you attempt to access an index that is out of the range of a particular array or string, an Index Out Of Bounds Exception will be thrown. For example, if you create and instantiate an array with length 12, and then attempt to read from index 15, an exception will be thrown.
-
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images