正在加载图片...
编程红宝书(珍藏版) ava 完金自学手 1.2异常示例 为了给读者一个直观的认识,在11.1节介绍的异常概念的基础上,给出 个运行期异常的例子。该程序中有除0错误,即两个整数相除,分母为0异 常,观察编译器的行为和执行该程序的结果。代码除0异常示例程序所示。 代码除0异常示例程序 1 public class ZeroException private int i=10; private int j=0 234567"89 private int r: private void getResulto i r=i/j System.out. println("i/j的计算结果是 public static void main(String[ args) 10 new ZeroExceptionO. getResulto Q想桃工业出隙社11.2 异常示例 • 为了给读者一个直观的认识,在11.1节介绍的异常概念的基础上,给出一 个运行期异常的例子。该程序中有除0错误,即两个整数相除,分母为0异 常,观察编译器的行为和执行该程序的结果。代码除0异常示例程序所示。 • 代码 除0异常示例程序 • 1 public class ZeroException{ • 2 private int i = 10; • 3 private int j = 0; • 4 private int r; • 5 private void getResult(){ • 6 r = i/j; • 7 System.out.println("i/j 的计算结果是: "+r); • 8 } • 9 public static void main(String[] args){ • 10 new ZeroException().getResult(); • 11 } • 12 }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有