首页 > 代码库 > 输出异常

输出异常

异常处理——抛出异常

代码如下:

package Day09;

public class TestInt { public static void main(String[] args) {
int num = Integer.MAX_VALUE-1;
System.out.println(num);
try {
num = intAddOne(num);
} catch (Exception e) {
e.printStackTrace();
}
System.out.print(num);
} private static int intAddOne(int number) throws Exception {
if (number >= Integer.MAX_VALUE) {
throw new Exception("Int max value reached");
}
return ++number;
} }

输出异常