一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识。

1)源代码

import javax.swing.*;

class AboutException {
public static void main(String[] a)
{
double i=-1, j=0, k;
k=i/j; 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 "+k);
//JOptionPane.showInternalConfirmDialog(null, k);
}
} }

2)结果截图

3)结果分析

Try{
   //可能发生运行错误的代码;
  }
  catch(异常类型     异常对象引用){
   //用于处理异常的代码
  }
  finally{
   //用于“善后” 的代码
  }

Java 中所有可捕获的异常都派生自 Exception 类。

二.使用Java异常处理机制

•把可能会发生错误的代码放进try语句块中。
•当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。
•当异常发生时,程序控制流程由try语句块跳转到catch语句块。
•不管是否有异常发生,finally语句块中的语句始终保证被执行。
•如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

三.动手动脑  多层的异常捕获-1

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)结果截图

3)结果分析
抛出两次异常,第一次处理完毕后,处理第二次。

四.动手动脑  多层的异常捕获-2

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

2)结果截图

3)结果分析

一个异常匹配结束后,才能执行下一个异常,系统不能累积处理异常。

五.动手动脑  当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

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");
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)结果截图

3)结果分析

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

六.动手动脑  finally语句块一定会执行吗?

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)结果分析

不一定。因为当运行 System.exit(0);时,终止了JAVA虚拟机,导致不能执行finally的内容。

七.动手动脑

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

import java.util.*;
public class Prograss {
public static void main(String[] args) { Scanner sc=new Scanner(System.in);
String pro;
System.out.println("请输入成绩:");
while(true){
try{
pro=sc.nextLine();
if(pro.matches("\\D*")){
throw new InException("输入有误!不是的不是数字!");
}
else{
int proo=Integer.parseInt(pro);
System.out.print("输入正确!");
judge(proo);
break;
}
}
catch(InException e){
System.out.print("请重新输入:");
}
}
sc.close();
} public static void judge(int pro){
if(pro>=0&&pro<60)
System.out.println("成绩不合格!");
else if(pro<70)
System.out.println("成绩合格!");
else if(pro<80)
System.out.println("成绩中!");
else if(pro<90)
System.out.println("成绩良!");
else if(pro<=100)
System.out.println("成绩优!");
else
System.out.println("输入成绩不符");
}
} class InException extends Exception{//
public InException(String msg){
super(msg);
}
}

JAVA语法基础作业——动手动脑以及课后实验性问题 (八)的更多相关文章

  1. JAVA 数组作业——动手动脑以及课后实验性问题

    JAVA课后作业——动手动脑 一:阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 1.源代码 // PassArray.java // ...

  2. JAVA 多态和异常处理作业——动手动脑以及课后实验性问题

    1.  阅读以下代码(CatchWho.java),写出程序运行结果: 1)  源代码 public class CatchWho { public static void main(String[] ...

  3. JAVA 接口与继承作业——动手动脑以及课后实验性问题

    一.继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构 ...

  4. JAVA类与对象作业——动手动脑以及课后实验性问题

    一: 1.源代码 //MAO2015.10.6 //foo class Foo { int value; public Foo(int initValue) { value = initValue; ...

  5. JAVA String作业——动手动脑以及课后实验性问题

    一:如何解释这样的输出结果?从中你能总结出什么?1.源码 //mao2015.10.20 //==的两个分辨 //对原始数据而言 //对引用类型变量 public class StringPool { ...

  6. 00JAVA语法基础_动手动脑

    1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本类.枚举目的就是要让某个变量的取值只能为若干固定值中的 ...

  7. 2019-9-16 java上课知识整理总结(动手动脑,课后实验)

    java上课知识整理总结(动手动脑,课后实验) 一,课堂测试 1,题目:课堂测试:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求:(1)题目避免重复: (2)可定制( ...

  8. Java语法基础(1)

    Java语法基础(1) 1.      Java是一门跨平台(也就是跨操作系统)语言,其跨平台的本质是借助java虚拟机 (也就是JVM(java virtual mechinal))进行跨平台使用. ...

  9. java语法基础--动手动脑问题及课后实验问题

    ---恢复内容开始--- 动手动脑: 1:仔细阅读示例:EnumTest.java,运行它,分析运行结果 结果 :枚举类型是引用类型!枚举类型不属于原始数据类型,它的每个具体指都引用一个特定的对象.相 ...

随机推荐

  1. linux共享windows文件并自动化改变文件编码

    以k3日志为例: 在k3的数据库服务器进行如下操作: 1.在k3的数据库服务器导出日志数据到本地D:/K3LOG下(脚本自动化执行) 2.设置脚本定时任务每天拷贝D:/K3LOG下的文件到D:/K3L ...

  2. Usual tiny skills & solutions

    Ubuntu and Win10 - double OS 2016-02-21 Yesterday I helped my friend install Ubuntu (14.04 LTS) on h ...

  3. Resources.Load加载文件返回null的原因

    1.文件夹都要放在Resources目录下 2.加载时photoName不需要扩展名 Texture2D t = Resources.Load<Texture2D>("Loadi ...

  4. Selenium-java-web常用操作---2

    都是些的方法,一起交流交流 上传文件 private static void action2() { // TODO Auto-generated method stub WebElement ele ...

  5. HTML 学习笔记 (drag & drop)

    拖放(Drag & Drop)是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.过去,我们用监听鼠标的Mousedown.Mouseo ...

  6. Parseval's theorem 帕塞瓦尔定理

    Source: wiki: Parseval's theorem As for signal processing, the power within certain frequency band = ...

  7. PCD文件去除曲率的脚本

    在写一个重建算法的时候需要用到点坐标和法向的数据文件,于是向利用pcl中的法向计算模块来生成法向.输出后法向文件中包含曲率信息,但是这是不需要的.于是自己写了一个python小脚本实现格式转换. #- ...

  8. (原创)使用VMware安装Ubuntu,怎么无法使用startx进入桌面模式?

    最近在VMware中安装Ubuntu时,发现VMware的快速安装后是文本模式,无法使用startx进入桌面模式,非常不方便.此问题为默认安装方式,需要设置安装方式. 操作系统:Windows 8.1 ...

  9. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. 让Visual Studio 2013为你自动生成XML反序列化的类

    Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...