Introduction to Exception Handlers – Java tutorial

Introduction to Exception Handlers – Java tutorial

An exception is an unexpected condition. To know how to create exceptions, you gotta learn to expect the unexpected. Most common errors are math error. Consider the program below:public class MathError
{
  public static void main(String[] args) throws ArithmeticException
  {
    int num = 13, denom = 0, result;
    result = num / denom;
  }
}

After compiling and running the program, you’ll get an error message that you are dividing by zero. Seeing as everyone knows you can’t possibly divide by zero, if this were a bigger program, it would be difficult to debug the program without a statement showing you what the problem is. This is where the try and catch blocks come in handy. Here’s the refined program:

Introducion to Exception Handlers

public class MathError2
{
  public static void main(String[] args) throws ArithmeticException
  {
    int num = 13, denom = 0, result;
    try
    {
      result = num / denom;
    }
    catch(ArithmeticException error)
    {
      System.out.println("Attempt to divide by zero!!");
    }
  }
}

Now, instead of an error message, you get message Attempt to divide by zero!! that you created in the catch block. To let Java find the error, you can use the Exception getMessage() method. To do this, remove the line System.out.println(“Attempt to divide by zero!!”); in the catch block and replace it with the line System.out.println(“The official message is ” + error.getMessage());. Now you should get the message The official message is / by zero. You can also have the catch block correct the the error by adding the following statements: System.out.println(“Denominator corrected to 1”); that shows how you are going to fix the problem, followed by the line result = num / 1; that recalculates the equation to be correct, then ending with the line System.out.println(“Result is ” + result); to display your changes. You can also test for more than one error. This next program will check to see if an arithmetic error occurred as well as an out-of-bounds error when trying to index an array.

public class TwoErrors
{
  public static void main(String[] args) throws ArithmeticException, IndexOutOfBoundsException
  {
    int num = 13, denom = 0, result;
    int[] array = {22,33,44};
    try
    {
      result = num / denom; // First try
      result = array[num]; // Second try
    }
    catch(ArithmeticException error)
    {
      System.out.println("Arithmetic error!!");
    }
    catch(IndexOutOfBoundsException error)
    {
      System.out.println("Index error!!");
    }
  }
}

The errors that occur are that you cannot divide any number by 0, nor can you reference any number in an array with a number that doesn’t match.

Question: I think I need a time machine! How do I know what I’m going to type as an error? What about what the user types? Is there a way to “catch” user input?

Answer: Slow down! We’re about to learn more in the next lesson. Next, we’ll learn about method Exceptions, handling the same Exceptions uniquely, and how to create your own Exception subclasses.