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");
}
}
}
运行结果:
运行结果:首先只有与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动手动脑——多态和继承
Java动手动脑——继承和多态 实验一 预估输出答案:100 200 201 202 输出结果:100 200 201 202 输出答案分析:100 创建parent类的对象,调用对象的方 ...
- java动手动脑和课后实验型问题String类型
1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...
- java 动手动脑7
---恢复内容开始--- 一.动手动脑:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果: ArrayIndexOutOfBoundsException/内层try-c ...
- java动手动脑和动手实验
动手动脑一: EnumTest.java: 程序代码: public class EnumTest { public static void main(String[] args) { Size s= ...
- java动手动脑和课后实验型问题第四讲
1.完全"手写代码实现"随机数生成 动手动脑: 编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. Modulus=231-1=int.MaxValue Mult ...
- Java动手动脑第四讲课堂作业
动手动脑1 完全“手写代码实现”随机数生成 纯随机数发生器
- JAVA动手动脑多态
动手实验一:下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d; d=m; d=(Dog)m; d=c; c=(Cat)m; 先进行自我判断,得出结论后,运行TestCas ...
- JAVA动手动脑
1.运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...
- java动手动脑和课后实验型问题
1.以下代码的输出结果是什么?为什么会有这个结果? int[] a = { 5, 7, 20 }; System.out.println("a数组中的元素:"); // 循环输出a ...
随机推荐
- sql存储过程比sql语句执行慢很多
参数嗅探的问题 原因:(1)可能是发生了参数嗅探,第一次赋给存储过程的输入参数,会为该存储过程生成一个基于输入参数的执行计划,因此如果第一次输入的参数不具有代表性(例如大部分查询输入的参数都是A值,但 ...
- 【转】Checkpoint--与lazy writer区别
checkpoint目的是减少数据库的恢复时间(服务奔溃或重启服务后的恢复),而lazy writer的目的是保证SQL OS 有空闲缓存块和系统有一定可用内存. Checkpoint和lazyW ...
- HQL查询——HQL查询的基本用法
HQL查询--HQL查询的基本用法 1.HQL语法类似于SQL语法,但是需要注意的是,HQL是一种完全面向对象的查询语言.SQL语言操作的对象是数据表.列等数据库对象,而HQL语言的操作对象是类.实例 ...
- iOS Library not loaded: Reason: image not found
iOS Library not loaded: ReactiveCocoa.framework ...Reason: image not found 解决办法:BuildPhases中,将对应的框架的 ...
- ip封包
I P封包 從一直以來討論至今﹐我們都不斷地接觸到“封包”這個詞﹐相信您也很有興趣想知道這個“封包”究竟是個什麼樣的東東吧﹗下面就讓我們一起看看一個IP封包究竟包含了那些內容. 擷取IP封包 如果您的 ...
- Example For maven-compiler-plugin
1. Compiling Sources Using A Different JDK The compilerVersion parameter can be used to specify the ...
- 《C++ Primer》学习笔记【第三部分 类设计者的工具】
第13章 拷贝控制 使用default:=defult只能修饰默认构造函数或拷贝控制成员,显式地要去编译器生成合成的版本. 使用delete:=delete通知编译器不希望定义这些成员,禁止试图使用它 ...
- js实现对移动设备的检测
<script type="text/javascript"> if (browserRedirect()) { location.href = 'http:/phon ...
- CSS选择器--普通选择器
普通选择器: 1.标签选择器:使用标签选择器,所有的相同的标签都会被选中.(如:选择div所有的div都被选中.) 2.类选择器:如果一个元素设置了多个类选择器样式,那么这些类选择器都会被设置.但是如 ...
- windows字符串
CString在win32环境下最大的有效长度应该是INT_MAX-1 一般小于这个长度的文件,处理字符串都没问题. TCHAR字符串数组没有处理子串的相关函数,strchr(_tcschr)只是处理 ...