我是靠谱客的博主 活力学姐,这篇文章主要介绍6-15 jmu-Java-06异常-多种类型异常的捕获 (10分),现在分享给大家,希望可以做个参考。

如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。

补全下列代码,使得程序正常运行。

裁判测试程序:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String choice = sc.next(); try { if (choice.equals("number")) throw new NumberFormatException(); else if (choice.equals("illegal")) { throw new IllegalArgumentException(); } else if (choice.equals("except")) { throw new Exception(); } else break; } /*这里放置你的答案*/ }//end while sc.close(); }

###输出说明
在catch块中要输出两行信息:
第1行:要输出自定义信息。如下面输出样例的number format exception
第2行:使用System.out.println(e)输出异常信息,e是所产生的异常。

输入样例

复制代码
1
2
number illegal except quit

输出样例

复制代码
1
2
3
4
5
6
7
number format exception java.lang.NumberFormatException illegal argument exception java.lang.IllegalArgumentException other exception java.lang.Exception
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*这里放置你的答案*/ catch(NumberFormatException e) { System.out.println("number format exception"); System.out.println(e); } catch(IllegalArgumentException e) { System.out.println("illegal argument exception"); System.out.println(e); } catch(Exception e) { System.out.println("other exception"); System.out.println(e); }

最后

以上就是活力学姐最近收集整理的关于6-15 jmu-Java-06异常-多种类型异常的捕获 (10分)的全部内容,更多相关6-15内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部