Exceptions

Traditional programming languages set flags or return bad values like -1 to indicate problems. Programmers often don't check these values.

Java throws Exception objects to indicate a problem. These cannot be ignored.

try-catch-finally

public class HelloThere {

  public static void main(String[] args) {
  
    try {
      System.out.println("Hello " + args[0]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Hello Whoever you are.");
    }
    finally {
      System.out.println("How are you?");  
    }
  
  
  }

}
Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.


Previous | Next | Top
Last Modified July 14, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu