Java基础知识强化之IO流笔记02:try...catch的方式处理异常

1. 案例示例:

 1 package com.himi.trycatch;
 2 
 3 public class ExceptionDemo {
 4 
 5     public static void main(String[] args) {
 6         int a = 2;
 7         int b = 0;
 8         try {
 9             System.out.println(a / b);
10         } catch (ArithmeticException ae) {
11             System.out.println("除数不能为0");
12         }
13         System.out.println("over");
14 
15     }
16 
17 }

运行结果如下:

 

     从上面的运行结果可以知道,暴露了第9行代码可能出现的问题。但是这个问题捕获到了提示给用户,让用户知道编写的代码出现的是怎样的问题,方便用户修正代码。同时不会因为第9行代码的问题影响下面的正常代码的运行。

posted on 2015-09-28 15:26  鸿钧老祖  阅读(335)  评论(0编辑  收藏  举报

导航