JAVA 多态和异常处理作业——动手动脑以及课后实验性问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA 多态和异常处理作业——动手动脑以及课后实验性问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3255字,纯文字阅读大概需要5分钟。
内容图文
1、 阅读以下代码(CatchWho.java),写出程序运行结果:
1) 源代码
public class CatchWho
{
public static void main(String[] args)
{
try
{
try
{
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e)
{
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
2) 运行结果
1、 写出CatchWho2.java程序运行的结果
1、1)源代码
public class CatchWho2
{
public static void main(String[] args)
{
try
{
try
{
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e)
{
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e)
{
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
1) 运行结果
3、请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
1)源代码
public class EmbededFinally
{
public static void main(String args[])
{
int result;
try
{
System.out.println("in Level 1");
try
{
System.out.println("in Level 2");// result=100/0; //Level 2
try
{
System.out.println("in Level 3");
result=100/0; //Level 3
}
catch (Exception e)
{
System.out.println("Level 3:" + e.getClass().toString());
}
finally
{
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e)
{
System.out.println("Level 2:" + e.getClass().toString());
}
finally
{
System.out.println("In Level 2 finally");
}
// result = 100 / 0; //level 1
}
catch (Exception e)
{
System.out.println("Level 1:" + e.getClass().toString());
}
finally
{
System.out.println("In Level 1 finally");
}
}
}
2)运行结果
1) 总结
先执行try语句,有catch执行,最后执行finally语句。
4、辨析:finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题
1)源代码
public class SystemExitAndFinally {
public static void main(String[] args)
{
try{
System.out.println("in main");
throw new Exception("Exception is thrown in main");
//System.exit(0);
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
finally
{
System.out.println("in finally");
}
}
}
2)运行结果
3)总结
Finally语句不一定执行,在catch语句中,有exit(0)语句即退出。要看具体情况,catch语句的处理。
5、编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。要求程序必须具备足够的健壮性,不管用户输入什么样的内容,都不会崩溃。
1)源代码
import java.io.*;
public class Grade {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = null;
int g;
BufferedReader strin =new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("请输入成绩(整数): ");
a = strin.readLine();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
try
{
g = Integer.parseInt(a);
if(g<=100&&g>=90)
System.out.println("优秀");
else if(g>=80&&g<90)
System.out.println("良好");
else if(g>=70&&g<80)
System.out.println("中");
else if(g>=60&&g<70)
System.out.println("及格");
else if(g<60)
System.out.println("不及格");
else
System.out.println("分值错误");
}
catch(NumberFormatException e)
{
System.out.println("输入错误");
}
}
}
2)运行结果
原文:http://www.cnblogs.com/justmaomao/p/4963399.html
内容总结
以上是互联网集市为您收集整理的JAVA 多态和异常处理作业——动手动脑以及课后实验性问题全部内容,希望文章能够帮你解决JAVA 多态和异常处理作业——动手动脑以及课后实验性问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。