As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception.
In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most Exceptions, that you will work with to control the flow of your program will be checked Exceptions.
Unchecked Exceptions are thrown at runtime and often mean, that you've made a mistake somewhere but the compiler couldn't know that.
All unchecked Exceptions extend RuntimeException, while all checked Exceptions don't.
There are a few Exceptions, that you will get ever so often. I've tried to make a list of the most common ones and to explain, what you can do to find, where it comes from. You should always look at the message you get from the compiler or at runtime - it will give you the line, in which it encountered the error.
- ArrayIndexOutOfBoundsException
This Exception, as most of them do and all of them should, describes what has gone wrong very well - some Array index is out of the given bounds. So, for example, you have an Array like thisExpand|Select|Wrap|Line Numbers- int [] array = new int[3]
Expand|Select|Wrap|Line Numbers- array[3]
Often you get this error, when using a for-loop which runs to often. Check the given line and find out, why it's trying to find an element, that doesn't exist.
The ArrayIndexOutOfBoundsException is a unchecked Exception. - ClassCastException
If you encounter a ClassCastException, there is an invalid cast somewhere. For example, the following code throws a ClassCastException:
Expand|Select|Wrap|Line Numbers- public class Exceptions {
- public static void main(String[] args) {
- // Anything extends java.lang.Object directly or indirectly and is therefore an Object
- Object testClass1 = new Class1("1");
- Object testClass2 = (Class2) testClass1;
- }
- }
- class Class1
- {
- String x;
- public Class1(String x)
- {
- this.x = x;
- }
- }
- class Class2
- {
- int x;
- public Class2(int x)
- {
- this.x = x;
- }
- }
Expand|Select|Wrap|Line Numbers- public class Exceptions {
- public static void main(String[] args) {
- // Now we've defined the cast ourselves and don't need to define the new Objects as Objects.
- Class1 testClass1 = new Class1("1");
- Class2 testClass2 = testClass1.toClass2();
- // If we do want to define them as Objects, it would work like this:
- Object testClass1_2 = new Class1("1");
- Object testClass2_2 = ((Class1) testClass1_2).toClass2();
- }
- }
- class Class1
- {
- String x;
- public Class1(String x)
- {
- this.x = x;
- }
- public Class2 toClass2()
- {
- return new Class2(Integer.parseInt(x));
- }
- }
- class Class2
- {
- int x;
- public Class2(int x)
- {
- this.x = x;
- }
- }
- ClassNotFoundException
This means, that a certain class cannot be found. Normally this happens, when you compile classes and try to run them without having compiled other classes, which are needed. It can also mean, that the needed classes are compiled, however not up to date. Try recompiling everything you need. - FileNotFoundException
When you try to access a File, it may not exist. This code will throw a FileNotFoundException:
Expand|Select|Wrap|Line Numbers- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- public class Exceptions {
- public static void main(String[] args) throws FileNotFoundException {
- File file = new File("hello.txt");
- FileInputStream iStream = new FileInputStream(file);
- }
- }
Expand|Select|Wrap|Line Numbers- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- public class Exceptions {
- public static void main(String[] args){
- try
- {
- File file = new File("hello.txt");
- FileInputStream iStream = new FileInputStream(file);
- }
- catch(FileNotFoundException fnfe)
- {
- System.err.println(fnfe);
- }
- }
- }
- IOException: The Input-Output-Exception is an Exception, which is very commonly used. The FileNotFoundException is one these and can therefore be caught with:
Expand|Select|Wrap|Line Numbers- catch(IOException ioe) {
- //...
- }
Expand|Select|Wrap|Line Numbers- public static void throwIOException() throws IOException
- {
- try
- {
- OutputStream oStream = new FileOutputStream(new File(""));
- oStream.close();
- oStream.write(0);
- }
- // The FileNotFoundException extends IOException - however to get a basic
- // IOException, we do this:
- catch(IOException fnfe)
- {
- IOException ioe = new IOException();
- ioe.initCause(fnfe);
- throw ioe;
- }
- }
- A wrapped exception - the try-catch-block catches one exception and throws another. This can be very useful in some cases.
- Creating a chained Exception. This means, the Exception carries information about where it comes from (i.e. the "FileNotFoundException"). The adding of this information is done with the line ioe.initCause(fnfe); and it can be retained with
Expand|Select|Wrap|Line Numbers- catch(IOException ioe) {
- {
- System.err.println(ioe);
- System.err.println("Cause: " + ioe.getCause());
- }
- NullPointerException
A NullPointerException is thrown, when an Object has been defined, but not declared when you try to access it. To use the last example again, this code will throw a NullPointerException:
Expand|Select|Wrap|Line Numbers- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- public class Exceptions {
- public static File file;
- public static void main(String[] args){
- try
- {
- FileInputStream iStream = new FileInputStream(file);
- }
- catch(FileNotFoundException fnfe)
- {
- System.err.println(fnfe);
- }
- }
- }
The NullPointerException is a unchecked Exception.
Back to chapter 2 or Continue to chapter 4