Java(异常处理)动手动脑
1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。
import javax.swing.*;
class AboutException {
public static void main(String[] a)
{
float i=1, j=0, k;
k=i/j;
System.out.println(k);
try
{
k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}
catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}
catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());
}
}
finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}
异常处理:Java中异常捕获语句
try{用于监控可能发生错误的语句}
catch(异常类型 异常对象引用)
{ 用于捕获并处理异常的代码 }
finally
{ //用于“善后” 的代码 }
不管是否有异常发生,finally语句块中的语句始终保证被执行。
2>阅读以下代码(CatchWho.java),写出程序运行结果:
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");
}
}
}
运行结果:
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

结果分析:当内层捕获异常并处理后,外层则不再捕获该异常。
3>写出CatchWho2.java程序运行的结果
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");
}
}
}
运行结果:
ArrayIndexOutOfBoundsException/外层try-catch

结果分析:当异常未被处理时无法接受新的异常。
4>请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
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");
}
}
}
运行结果:


结果分析:当外层异常未被处理时,内层异常不会被处理并且finally也不会执行,当有多层嵌套的finally语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序。
5>finally语句块一定会执行吗?
请通过 SystemExitAndFinally.java示例程序回答上述问题
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");
}
}
}
运行结果:
System.exit(0);

//System.exit(0);

运行结果:首先只有与finally对应的try语句得到执行的情况下finally语句才会执行,但如果finally语句之前出现例如System.exit(0) 等使Java虚拟机停止运行的语句时finally语句也不会被执行。
6>编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
import java.util.Scanner;
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
class GradeTest{
private double grade;
public void InputGrade(String s)
{
boolean flag = true;
try
{
for(int i = 0;i<s.length();i++)
{
if((s.charAt(i) < '0' || s.charAt(i) > '9')&& s.charAt(i) != '.')
flag = false;
}
if(!flag)
{
throw new MyException("请输入数字!");
}
try{
grade = Double.parseDouble(s);
if(grade < 0.0 || grade > 100.0)
{
throw new MyException("请输入0.0到100.0的数据!");
}
if(grade < 60.0)
System.out.println("不及格");
else if(grade <70.0)
System.out.println("及格");
else if(grade < 80.0)
System.out.println("中");
else if(grade < 90.0)
System.out.println("良");
else
System.out.println("优");
}
catch(MyException e)
{
System.out.println(e.getMessage());
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
}
}
}
public class Grade { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
GradeTest G = new GradeTest();
System.out.println("请输入成绩:");
String str = in.nextLine();
G.InputGrade(str);
in.close();
} }
运行结果:




Java(异常处理)动手动脑的更多相关文章
- java异常处理动手动脑问题解决和课后总结
动手动脑 一.问题:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. 1.源代码 import javax.swing.*; cl ...
- Java之动手动脑(三)
日期:2018.10.12 星期五 博客期:017 这次留了两个动手动脑作业!我需要一个一个来说!先说第一个吧! Part 1 :随机生成1000个随机数 代码: //以下为 RandomMaker. ...
- java的动手动脑10月20日
(1)动手动脑 该函数没有赋初值再就是如果类提供一个自定义的构造方法,将导致系统不在提供默认的构造方法. (2) public class test { /*** @param args*/publi ...
- Java的动手动脑
动手动脑及课后实 仔细阅读示例: EnumTest.java,运行它,分析运行结果? public class EnumTest { public static void main(String[] ...
- java课java方法动手动脑
动手动脑: import java.util.Scanner; public class Random { public static void main(String[] args) { ...
- java课堂动手动脑及课后实验总结
动手动脑一:枚举 输出结果: false false true SMALL MEDIUM LARGE 分析和总结用法 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本 ...
- java多态与异常处理——动手动脑
编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”.“及格”.“中”.“良”.“优”的结论. 要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都 ...
- Java的动手动脑(六)
日期:2018.11.8 星期四 博客期:022 --------------------------------------------------------------------------- ...
- java-10异常处理动手动脑
1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutExcep ...
- Java一些动手动脑实验
一.Java字段初始化的规律: 输出结果为:100 和 300 当把{filed=200}放在public int field=100之后输出结果为:200 和 300 所以执行类成员定义时指定的默认 ...
随机推荐
- java httpclient cookie
BasicCookieStore cookieStore = new BasicCookieStore();BasicClientCookie cookie = new BasicClientCook ...
- git 查看远程分支、本地分支、删除本地分支【转】
1 查看远程分支 $ git branch -a * br-2.1.2.2 master remotes/origin/HEAD -> origin/master remotes/origin/ ...
- 服务器上搭建web环境
一.安装tomcat [root@localhost ~]# mkdir tomcat-src --新建文件夹 [root@localhost ~]# cd tomcat-src ...
- 完全卸载Oracle11G
要特别注意删除注册表的这块,如果删错了会导致系统出现问题,而且oracle的安装卸载真的很烦,一旦装错了,卸载不干净就会导致种种的问题无法再次安装,个人建议用360卸载,360卸载完成后会自动检测到无 ...
- zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法
转载自:http://blog.csdn.net/wide288/article/details/21622183 zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法: ze ...
- <读书笔记>软件调试之道 :从大局看调试-理想的调试环境
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记.欢迎转载! ---------------------------------------- ...
- iOS 6.0之后支持一个页面横屏的方法
拿两个页面举例子: 0.开启旋转: 1.第一个界面实现以下方法 /** * 默认显示的方向,preferredInterfaceOrientationForPresentation必须返回一个支持的接 ...
- springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...
- JS的封装
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAl8AAADiCAYAAABwdKKfAAAYLGlDQ1BJQ0MgUHJvZmlsZQAAWIWVeQ
- 安装XMind
XMind是一款思维导图软件.可编辑整理头脑中的想法,以使其脉络更加清晰. 在学习知识过程中,用这个工具也不错. 官方网站: http://www.xmind.net 其它版本: http://ww ...