tryexception.java

来自「Java 入门书的源码」· Java 代码 · 共 24 行

JAVA
24
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Puts the array code in a try block and
 * catches the array index out of bounds exception
 * if it occurs.
 */

import iopack.Io;
public class TryException{
  public static void main(String [] args) {
    int value;
    try {
      int [] anArray = {5,6,7};
      int index = Io.readInt("Enter an index from 0 to 2"); 
      value = anArray[index];
      System.out.println("Execution does not get here if index is bad");
    }catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Stick with 0, 1, or 2");
    }
    System.out.println("This is the end of the program");
    Io.readString("Press any key to exit");   // Added for IDE use
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?