What's the difference between a checked and an unchecked exception?
Author: Deron Eriksson
Description: This tutorial describes the differences between checked and unchecked exceptions in Java.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page: < 1 2

(Continued from page 1)

Now, lets look at some examples of using CheckedException and UncheckedException. I'll start with CheckedException. Suppose we write a class called ExceptionTest that consists of the following code. We throw a new CheckedException.

package test;

public class ExceptionTest {
	public static void main(String[] args) {
		throw new CheckedException("This has to be caught or thrown");
	}
}

This code has an "Unhandled exception type CheckedException" error, as shown below:

Unhandled exception type CheckedException

We can correct this error by catching CheckedException:

package test;

public class ExceptionTest {
	public static void main(String[] args) {
		try {
			throw new CheckedException("This has to be caught or thrown");
		} catch (CheckedException e) {
			e.printStackTrace();
		}
	}
}

The output of the above code is shown here:

test.CheckedException: This has to be caught or thrown
	at test.ExceptionTest.main(ExceptionTest.java:6)

Instead of catching CheckedException, we could also have our main method throw it, as in the following code:

package test;

public class ExceptionTest {
	public static void main(String[] args) throws CheckedException {
		throw new CheckedException("This has to be caught or thrown");
	}
}

This generates the following output:

Exception in thread "main" test.CheckedException: This has to be caught or thrown
	at test.ExceptionTest.main(ExceptionTest.java:5)

Now, let's look at UncheckedException. Suppose we have the following code that throws UncheckedException:

package test;

public class ExceptionTest {
	public static void main(String[] args) {
		throw new UncheckedException("We're not forced to catch this one");
	}
}

Notice that we don't need to catch or specify that our method throw UncheckedException, like we did with CheckedException. This is because UncheckedException subclasses RuntimeException and thus is an unchecked exception.

Unchecked exception does not need to be caught

Executing the code above generates the following output.

Exception in thread "main" test.UncheckedException: We're not forced to catch this one
	at test.ExceptionTest.main(ExceptionTest.java:5)

Of course, we can catch UncheckedException if we'd like to, as in the following code:

package test;

public class ExceptionTest {
	public static void main(String[] args) {
		try {
			throw new UncheckedException("We're not forced to catch this one");
		} catch (UncheckedException e) {
			e.printStackTrace();
		}
	}
}

The above code generates the following output.

test.UncheckedException: We're not forced to catch this one
	at test.ExceptionTest.main(ExceptionTest.java:6)

If you can predict that a particular bug may arise in a certain situation, typically you use a checked exception. However, if a situation arises where you can't really predict that a bug will happen, this tends to be a situation where you might use an unchecked exception.

Page: < 1 2