1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

import javax.swing.*;

class AboutException {
public static void main(String[] a)
{
int 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");
} }
}

把可能会发生错误的代码放进try语句块中。 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。 当异常发生时,程序控制流程由try语句块跳转到catch语句块。 不管是否有异常发生,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");
}
}
}

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

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

3.辨析: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"); } } }

因此finally语句不一定执行

4.如何跟踪异常的传播路径?

// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {
public static void main( String args[] )
{
try {
method1();
}
catch ( Exception e ) {
System.err.println( e.getMessage() + "\n" );
e.printStackTrace();
}
} public static void method1() throws Exception
{
method2();
} public static void method2() throws Exception
{
method3();
} public static void method3() throws Exception
{
throw new Exception( "Exception thrown in method3" );
}
}

当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

5.成绩:

import java.util.Scanner;

class judgeException extends Exception{
public judgeException(String s){
super(s);
}
}
class judgeException2 extends Exception{
public judgeException2(String s){
super(s);
}
}
public class Test { public static void main(String[] args) {
boolean p=true;
while(p){
System.out.println("请输入学生的分数:");
Scanner in=new Scanner(System.in);
String s=in.next();
try{
for(int i=0;i<s.length();i++){
if((s.charAt(i)<48||s.charAt(i)>57)){
throw new judgeException("输入错误,请输入一个数字:");
}
}
try{
int m=Integer.parseInt(s);
if(m<0||m>100){
throw new judgeException2("请输入正确的分数");
}
if(m<60){
System.out.println("不及格");
}
else if(m>=60&&m<=69){
System.out.println("及格");
}
else if(m>=70&&m<=79){
System.out.println("中");
}
else if(m>=80&&m<=89){
System.out.println("良");
}
else if (m>=90&&m<=100){
System.out.println("优");
}
}
catch(judgeException2 e){
System.out.println(e.getMessage());
} }
catch (judgeException e){
System.out.println(e.getMessage());
}
}
}
}

java-10异常处理动手动脑的更多相关文章

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

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

  2. java异常处理动手动脑问题解决和课后总结

    动手动脑 一.问题:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. 1.源代码 import javax.swing.*; cl ...

  3. java第二节课 java语法基础动手动脑

    动手动脑1:枚举变量   运行EnumTest.java package test2; public class EnumTest {  public static void main(String[ ...

  4. java学习——关于java课件上动手动脑问题简单的分析

    问题一:关于以下的代码为什么会产生错误的问题的简单分析. 第一个动手动脑提供了一下的代码,可以发现,在Foo的这个类中只定义了一个Foo(int)类型的构造函数,在之前的学习工程中,我们并没有接触到j ...

  5. java语言课堂动手动脑

    1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

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

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

  7. java学习(4)——动手动脑

    根据ppt所给的例子,运行的结果如下所示: ppt中出现的第二个动手动脑如下: 代码如下: 其运行结果如下: 作出简单的分析如下:有点类似于if 和else的关系,总是对应try和catch同层中的异 ...

  8. java多态与异常处理——动手动脑

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

  9. java文件课后动手动脑

    package 第九周作业1; import java.io.File; import java.io.FileInputStream; import java.io.IOException; imp ...

  10. JAVA课堂练习-动手动脑--数组

    1.阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 源代码: public class PassArray { public stat ...

随机推荐

  1. Hibernate深入浅出(九)持久层操作——数据保存&批量操作

      数据保存: 1)session.save session.save方法用于实体对象到数据库的持久化操作.也就是说,session.save方法调用与实体对象所匹配的Insert SQL,将数据插入 ...

  2. python 课堂笔记-if语句

    # Author:zyl _username = 'zyl' _password = 'zyl123' username = input("username:") password ...

  3. 【HackerRank】Ice Cream Parlor

    Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N ...

  4. java 跨数据库导入大数据

    java 跨数据库导入大数据 /** * java程序跨服务器跨数据库批量导入导出百万级数据 * @param args * @throws Exception */ public static vo ...

  5. 灰色3D按钮组合

    在线演示 本地下载

  6. 在shell,R,python中用变量和常量创建文件名

    很多时候我们希望文件名的格式是:变量+常量的. 1.shell:变量"常量" [wangjq@mgmt multi_pcr]$ a="var" [wangjq@ ...

  7. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  8. Android LCD(三):LCD接口篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/9177085 关键词:Android LCD控制器 Framebuffer PWM  平 ...

  9. Go Mysql驱动

    Golang中MYSQL驱动 Mysql库https://github.com/go-sql-driver/mysql Go本身不提供具体数据库驱动,只提供驱动接口和管理. 各个数据库驱动需要第三方实 ...

  10. centos 源码安装php5.5

    系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...