Exceptions

From Processing

Jump to: navigation, search

We've all experienced the following:

java.lang.ArrayIndexOutOfBoundsException

java.io.IOException: openStream() could not open file.jpg

java.lang.NullPointerException

It’s sad when these errors occur. Really, it is. The error message prints out and the program sputters to a halt, never to continue running again. Perhaps you have developed some techniques for protecting against these error messages. For example:

if (index < somearray.length) {
  somearray[index] = random(0,100);
}

The above is a form of “error checking.” The code uses a conditional to check that the index is valid before an element at that index is accessed. The above code is quite conscientious and we should all aspire to be so careful.

Not all situations are as simple to avoid, however, and this is where exception handling comes into play. Exception handling refers to the process of handling errors, out of the ordinary events that occur during the execution of a program.

The code structure for exception handling in Java is known as try catch. In other words, “Try to run some code. If you run into a problem, however, catch the error and run some other code.” If an error is caught using try catch, the program is allowed to continue. Let’s look at how we might rewrite the array index error checking code try catch style.

// The code that might produce an error goes within the “try” section.  
try {
  somearray[index] = 200;
// The code that should happen if an error occurs goes in the “catch” section.
} catch (Exception e){
  println("Hey, that’s not a valid index!");
}

The above code catches any possible exception that might occur. The type of error caught is the generic Exception. However, if you want to execute certain code based on a specific exception you can as the following code demonstrates.

// Different “catch” sections catch different types of exceptions.  
// In addition, each exception is an object and can therefore have methods called on it.  
// For example: "e.printStackTrace();" displays more detailed information about the exception.
 
try {
  somearray[index] = 200;
} catch (ArrayIndexOutOfBoundsException e){
  println ("Hey, that’s note a valid index!");
} catch (NullPointerException e) {
  println("I think you forgot to create the array!");
} catch (Exception e) {
  println("Hmmm, I dunno, something weird happened!");
  e.printStackTrace();
}

The above code, nevertheless, does nothing more than print out custom error messages. There are situations where we want more than just an explanation in which case you can write code to actually do something about the error, like

try {
  somearray[index] = 200;
} catch (ArrayIndexOutOfBoundsException e){
  index = somearray.length-1;
  somearray[index] = 200;
}

In many cases, however, you don't need exception handling, because you can just write some code to make sure that an error doesn't occur. For example, when loadStrings() fails to open a file or connect to a URL, it returns null. You could catch a NullPointerException or instead just say:

String[] lines;
String url = "http://wwww.rockinurl.com";
lines = loadStrings(url);
if (lines == null) {
  // If a problem occurs connecting to the URL, we’ll just fill the array with dummy text so that the sketch can still run.
  lines = new String[3];
  lines[0] = "I could not connect to " + url + "!!!"; 
  lines[1] = "But here is some stuff to work with"; 
  lines[2] = "anyway.";
}
 
println(lines)
Personal tools