⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tryblocktest.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.IOException;

public class TryBlockTest
{
  public static void main(String[] args)
  {
    int[] x = {10, 5, 0};              // Array of three integers

    // This block only throws an exception if method divide() does
    try
    {
      System.out.println("First try block in main()entered");
      System.out.println("result = " + divide(x,0));  // No error
      x[1] = 0;                        // Will cause a divide by zero
      System.out.println("result = " + divide(x,0));  // Arithmetic error
      x[1] = 1;                        // Reset to prevent divide by zero
      System.out.println("result = " + divide(x,1));  // Index error
    }
    catch(ZeroDivideException e)
    {
      int index = e.getIndex();        // Get the index for the error
      if(index > 0)                    // Verify it is valid
      {                                // Now fix up the array...
        x[index] = 1;                  // ...set the divisor to 1...
        x[index + 1] = x[index - 1];   // ...and set the result
        System.out.println("Zero divisor corrected to " + x[index]);
      }
    }
    catch(ArithmeticException e)
    {
      System.out.println("Arithmetic exception caught in main()");
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      System.out.println
             ("Index-out-of-bounds exception caught in main()");
    }
    System.out.println("Outside first try block in main()");
  }

  // Divide method
  public static int divide(int[] array, int index)
                                         throws ZeroDivideException
  {
    try
    {
      System.out.println("First try block in divide() entered");
      array[index + 2] = array[index]/array[index + 1];
      System.out.println("Code at end of first try block in divide()");
      return array[index + 2];
    }
    catch(ArithmeticException e)
    {
      System.out.println("Arithmetic exception caught in divide()");
      throw new ZeroDivideException(index + 1);  // Throw new exception
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      System.out.println(
                "Index-out-of-bounds index exception caught in divide()");
    }
    System.out.println("Executing code after try block in divide()");
    return array[index + 2];
  }

}

⌨️ 快捷键说明

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