一、动手动脑:多层的异常捕获-1

阅读以下代码(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

二、动手动脑:多层的异常捕获-2

阅读以下代码(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

三、请先阅读 EmbededFinally.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");

}

}

}

运行结果:

in Level 1

in Level 2

in Level 3

Level 3:class java.lang.ArithmeticException

In Level 3 finally

In Level 2 finally

In Level 1 finally

分析:当有多层嵌套的finally时,异常在不同的层次抛出  ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序

四、finally语句块一定会执行吗

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");

}

}

}

 

运行结果:

in main

Exception is thrown in main

 

分析:在JVM正常运行的情况下,finally块一定会执行。但如果JVM都退出了finally块就无法执行了

五、Java多层嵌套异常处理的基本流程

         http://lavasoft.blog.51cto.com/62575/18920/

http://blog.sina.com.cn/s/blog_9d88a5770101gsf4.html

 

六、成绩判断

源代码:

package 成绩判断;

import javax.swing.JOptionPane;

//<=60 “不及格”、60--69“及格”、70--79 “中”、80--89“良”、90-100“优”

class NumberlimitedExceptiion extends Exception{

NumberlimitedExceptiion(){

JOptionPane.showMessageDialog(null, "所输入的数字超出范围!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

}

class Shuru{

int score;

public static void prime(int m) throws NumberlimitedExceptiion{

if(m>100||m<0){

NumberlimitedExceptiion limitexcep=new NumberlimitedExceptiion();

throw limitexcep;

}

}

public void input(){

try{

String i=JOptionPane.showInputDialog("请输入一个整数成绩:");

score=Integer.parseInt(i);

}

catch(NumberFormatException e){

JOptionPane.showMessageDialog(null, "输入的不是整数!",

"输出",JOptionPane.INFORMATION_MESSAGE);

input();

}

try{

prime(score);

}

catch(NumberlimitedExceptiion e) {

input();

}

//      finally{

//

//      }

}

}

public class Juage {

public static void main(String[] args){

Shuru a=new Shuru();

a.input();

if(a.score<60){

JOptionPane.showMessageDialog(null, "此成绩为不及格!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

else if(a.score<=69){

JOptionPane.showMessageDialog(null, "此成绩为及格!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

else if(a.score<=79){

JOptionPane.showMessageDialog(null, "此成绩为中等!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

else if(a.score<=89){

JOptionPane.showMessageDialog(null, "此成绩为良等!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

else if(a.score<=100){

JOptionPane.showMessageDialog(null, "此成绩为优等!",

"输出",JOptionPane.INFORMATION_MESSAGE);

}

}

}

java07课堂作业的更多相关文章

  1. 栋哥你好,让我们回顾最初认识C++的时候(课堂作业)

    计算器的第一步,至今还记记忆犹新,本次的课堂作业,便是那个框架.闲话少叙,代码如下传送门: Main.cpp #include "stdafx.h" #include<ios ...

  2. 20155213 第十二周课堂作业MySort

    20155213 第十二周课堂作业MySort 作业要求 模拟实现Linux下Sort -t : -k 2的功能 参考 Sort的实现 提交码云链接和代码运行截图 初始代码 1 import java ...

  3. 课堂作业-Bag类的实现

    课堂作业-Bag类的实现 要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进 ...

  4. Java课程课堂作业代码

    前言 本文章只是单纯记录课堂老师布置的课堂作业代码,题目都比较简单,所以没有写解题思路,相信大家都能理解,当然其中有的解法和代码不是最优的,当时只是为了完成题目,后来也懒得改了,如果有不恰当或者不正确 ...

  5. Java课堂作业详解

    今天的Java课堂留下了一个作业:使用Eclipse编写一个程序,使输入的两个数进行加和,并且输出他们的和.对于这个题目,我们首先可以把它分解成为三个不同的小步骤 第一步就是输入这两个数,因为我们无需 ...

  6. 百度前端学院js课堂作业合集+分析(更新中...)

    第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  7. OSLab课堂作业1

        日期:2019/3/16 作业:实现命令cat, cp, echo. myecho命令 #include <stdio.h> int main(int argc, char *ar ...

  8. 面向对象程序设计_课堂作业_01_Circle

    The 1st classwork of the C++ program 题目: Create a program that asks for the radius of a circle and p ...

  9. C++ 课堂作业1.0

    c++第一次课堂作业点这里 题目要求:输入半径,计算圆的面积,在调用外部函数,无需使用类.

随机推荐

  1. EditorGUILayout.EnumPopup 枚举弹出选择菜单

    http://www.unity蛮牛.com/thread-25490-1-1.html http://www.unity蛮牛.com/m/Script/EditorGUILayout.EnumPop ...

  2. [BZOJ3572][Hnoi2014]世界树

    [BZOJ3572][Hnoi2014]世界树 试题描述 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条 ...

  3. android oom 全解析

    Android oom 有时出现很频繁,这一般不是Android设计的问题,一般是我们的问题. 就我的经验而言,出现oom,无非主要是以下几个方面: 一.加载对象过大 二.相应资源过多,没有来不及释放 ...

  4. (备忘)android清单文件中<meta-data>标签,以及<intent-filter>下的<data>标签及其他标签

    1.metadata可以写在application下也可以写在activity下,作为全局或activity内共享的数据 以键值对形式保存 <meta-data android:name=&qu ...

  5. BZOJ 1355: [Baltic2009]Radio Transmission

    Description 一个字符串最短周期. Sol KMP. 最短周期就是 \(n-next[n]\) 证明: 当该字符串不存在周期的时候 \(next[n]=0\) 成立. 当存在周期的时候 \( ...

  6. Android架构:用消息机制获取网络数据

    网络请求,不管是什么协议,是长连接还是短连接,总是一个异步的请求,过程包括:加请求参数->发起请求->接收响应->解析数据->获得业务数据. 最挫的做法是,业务代码包揽所有这些 ...

  7. ssh免密码登入

    通常做许多事情(git puh/脚本等等),不停输入密码是件很不愉快的事情,破解如下: http://www.linuxproblem.org/art_9.html 1. 生成rsa密钥 ssh-ke ...

  8. Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项

    在获取图片缩略图时,获取游标并进行相关的操作. Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbna ...

  9. ios CoreData NSManagedObject 生命周期

    用同样的检索条件从context检索出的对象是一个????所以 在主页的3个brand没法释放,在仅仅处理brand的时候???? 和 多个 context无关 我重写了NSManagedObject ...

  10. ACM/ICPC 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))

    两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...