7.1 处理错误
7.1.1 异常分类
7.1.2 声明受查异常
7.1.3 如何抛出异常
7.1.4 创建异常类
7.2 捕获异常
7.2.1 捕获异常
7.2.2 捕获多个异常
7.2.3 再次抛出异常与异常链
7.2.4 finally 子句
7.2.5 带资源的 try语句
7.2.6 分析堆栈轨迹元素
package stackTrace;
import java.util.*;
/**
* A program that displays a trace feature of a recursive method call.
* @version 1.01 2004-05-10
* @author Cay Horstmann
*/
public class StackTraceTest
{
/**
* Computes the factorial of a number
* @param n a non-negative integer
* @return n! = 1 * 2 * . . . * n
*/
public static int factorial(int n)
{
System.out.println("factorial(" + n + "):");
Throwable t = new Throwable();
StackTraceElement[] frames = t.getStackTrace();
for (StackTraceElement f : frames)
System.out.println(f);
int r;
if (n <= 1) r = 1;
else r = n * factorial(n - 1);
System.out.println("return " + r);
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
factorial(n);
}
}
7.3 使用异常机制的技巧