A checked exception is a type of exception that a compiler requires to be handled. One can think of a checked exception as being like a pop fly in baseball, where an exception is that the ball is hit at all, and the checking is the catching of the ball; you're essentially required to catch it if you can if you are a fielder.

Note: any code presented here may or may not be correct in terms of syntax; it is here merely to demonstrate the idea.

Completely lost? Let's start from the basics. An exception is the technical term for when the unexpected happens in a computer program. An example of an exception is some sort of program that expects to receive nothing but the letter "c" as input, and suddenly receives the letter "e"; an exception occurs here.

Basically, exceptions are designed to relieve a computer programmer of the need to check return codes or state variables after every function call to determine if something odd has occurred. Instead, whenever something odd happens, an exception occurs and a special piece of code called an exception handler takes over and deals with the problem. In the "c" example above, there might be an exception handler for the situation where the program receives an "e" as input, and that handler might tell the program to just interpret it as a "c" anyway or whatever the programmer decides should happen.

In the programming language Java, there are two types of exceptions: checked and unchecked. Checked exceptions are those that the compiler requires to be handled; in other words, checked exceptions are ones that Java cannot handle alone and thus need the programmer's help with, while Java can handle unchecked exceptions without help.

Checked exceptions in Java (we'll use Java as an example language here) are quite easy to deal with. All you have to do is set a trap within the code for a particular exception that might occur, and then throw it. Here's a really simple example.

public class Test
{ public void static main( String args ) throws BogusException
  { try {
      int i = 1;
      if (i == 1) throw BogusException; 
    } catch (BogusException e) {
      System.out.println("This is BOGUS!");
    } finally {
      System.out.println("Finally, we're done with this BOGUS!");
} } }

The first line defines a class Test; that's not important here. The second line says that this is the main function that we're going to be running and then at the end mentions that we might be throwing a BogusException, which is a particular kind of exception; you can define specific exception types if you like, and BogusException is one we've already defined (meaning compiling this piece of code would cause an error without the definition).

The third line has the word try, followed by a left curly brace; this means we're going to try the stuff until the next curly brace to see if anything happens. Inside the try braces is where we do stuff that might be exceptional.

The fourth and fifth lines basically exist to throw an exception in this example. The fifth line states that if i is 1 (and it is), then the program throws a BogusException. Now, since we've thrown this exception, we have to deal with it. That's what the catch() segment is about; if a particular kind of exception has occurred, it will catch whatever kind of exception is named inside the parentheses in front of it. Here, in line 6, we catch a BogusException, which has occurred; if it had not, we would skip this part inside the curly braces after that catch statement. So, we go on and print the line "This is BOGUS!" After that, we do the finally segment, which occurs whether you've thrown an exception or not; we also print the line specified there. And that's all there is to it!

Checked exceptions are the programmer's way of dealing with problems that occur; Java is just being used here as an example. Although other languages such as C and C++ don't directly offer checked exceptions and a way of dealing with them, a clever programmer can write the equivalent of the code and implement their own checked exception handling with some clever if loops inside the function.

There are several ways of dealing with a checked exception, each of which has benefits and drawbacks. Each of these has a multitude of uses as well, and they all belong inside the toolbox of a good programmer.

Suppression is the simplest strategy of handling a checked exception. Essentially, suppression is used whenever nothing else seems to fit, when an exception is thrown unintentionally or accidentally, or when you merely want nothing whatsoever to happen. In Java, suppression looks like this:

public void experiment() throws BogusException {
	try {
		throw BogusException;
	} catch (BogusException b) {
	}
}

In other words, a caught exception that is dealt with by suppression does nothing at all. You merely catch it simply so that further problems are not caused; you have no real response to it.

Bailing out is a slightly more complicated strategy than suppression. Instead of doing nothing, you simply exit the program. The reasons for handling the exception this way mostly involve situations in which you are extremely concerned that bad things might happen if you continue, or if you simply want to stop if something odd occurs. Here's a Java example.

public void experiment() throws BogusException {
	try {
		throw BogusException;
	} catch (BogusException b) {
		System.exit(1);
	}
}

Another way of handling an error is through propagation, which is essentially assuming that another piece of code (usually a piece provided by the compiler) will handle the situation. In this case, we write a BogusException class of its own with an exception handler in it, and we just let a thrown BogusException be dealt with there. So, all we would do in Java is...

public void experiment() throws BogusException {
	throw BogusException;
}

Another solution is that of a base case, but this is a very poor solution. This is basically propagation except without specifying the type of error thrown. Java provides you with a very generic type of exception called Exception, which handles things extremely simply. Only use this if you're unsure of the type of error and only want very minimal catching; it works exactly as the code above except replacing BogusException with Exception.

The most robust type of handling of checked exceptions is that of wrapping, which basically means that a class you write has its own way of dealing with every conceivable type of exception. A programmer doing a wrapping scheme will have a very well-controlled program, but it will require a significant amount of labor to complete.

However, the best scheme may be translation, in which different exception types are translated into others, by merely catching one type then throwing another, and only having a few specific types do anything else. This way, you can write specific instructions for most exception types, but only have to do it once, and then write a very simple translator.

Checked exceptions are a great way of handling errors in programs, especially in Java where the ability is already set up within the language itself. It is a valuable part of any programmer's skill set. As always, though, if you plan to use checked exceptions, do some further reading. A top quality Java book such as Core Java, published by Sun, should educate you properly on checked exceptions and how to handle them.

Log in or registerto write something here or to contact authors.