Lesson Nineteen

Advanced Exception Concepts


In earlier lessons, you've been able to "throw Exceptions" for your programs that have required user input. Usually this was done immediately after the declaration of the main() method. As with the main() method, you can throw Exceptions in other methods that you create. However, another program may already have a way of handling errors and will make error handling redundant or difficult to process. If you do choose to handle your own method exceptions, you need to know three things:

  • The method's return type
  • The type and number of arguments the method requires
  • The type and number of Exceptions the method throws

Although it may be redundant, there is a way of solving some problems using recursion. Recursion is a programming technique that solves problems through method calling. Recursive programming is a way of solving a problem by repeatedly solving smaller versions of the same problem. This will come almost naturally with those who are good with math. Remember "factorials"? Recursion works the same way. Take the factorial 5!. If you're not familiar with factorials, 5! is the same as 5 x 4 x 3 x 2 x 1, or 120. To show how this works in Java, consider the following program:

public class Factorial {
  public static int fact(int n) {
    int returnVal;
    if (n < 1) {
      throw new IllegalArgumentException("Illegal value: " + n);
    } else if (n == 1) {
      returnVal = n;
    } else {
      returnVal = n * fact(n-1);
    }
    return returnVal;
  }

  public static void main(String args[]) {
    for (int i=1; i<26; i++) {
      System.out.println(fact(i));
    }
  }
}

When developing programs, you may have a limited amount of space to work with. The program above may take up too much space and slow your program down, even if it's just milliseconds. There's a way to get the same results with less programming:

int fact = 1;
for (int i=2; i<26; i++) {
  fact *= i;
}
System.out.println("25! is " + fact);

You don't have to throw Error or RuntimeExceptions. The errors you received when you made mistakes are RuntimeExceptions. You can throw an InterruptedException when you are working with threads, which we will learn in later chapters. And, you already know a method can throw without catching because we've made programs that use keyboard input where we threw the Exception, but didn't create a catch block.

What you may not know is you can create your own Exceptions. For example, if you wanted to create your own Exception for the program above, you might call it FactoralException; where you might test if the equation is a true fatoral. If you do create your own Exceptions, end its name with Exception.

Question: Ok. Ready-made Exceptions and my own Exceptions. I get the picture. Now can we get on to things that make sense?

Answer: Sure. Now that you know how to make the programs work, let's see how others can make them work with their own inputs to files and the outputs they create.


Lesson Eighteen | Home | Lesson Twenty

If you have a question about any of the lessons, feel free to ask.

1