常见的第10章 异常处理机制.pptVIP

  • 5
  • 0
  • 约1.53万字
  • 约 49页
  • 2018-11-04 发布于山东
  • 举报
常见的第10章 异常处理机制

在try-catch块后加入finally块,可以确保无论是否发生异常,finally块中的代码总能被执行 try 块 finally 块 catch 块 无异常 有异常 try-catch-finally 四、异常处理 public class Test4 { public static void main(String[] args) { try { Scanner in = new Scanner(System.in); System.out.print(请输入被除数:); int num1 = in.nextInt(); System.out.print(请输入除数:); int num2 = in.nextInt(); System.out.println(String.format(%d / %d = %d, num1, num2, num1/ num2)); } catch (Exception e) { System.err.println(“出现错误:被除数和除数必须是整数, +除数不能为零。); System.out.println(e.getMessage()); } finally { System.out.println(感谢使用本程序!); } }

文档评论(0)

1亿VIP精品文档

相关文档