java catch getm_java-10异常处理动手动脑

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

import javax.swing.*;classAboutException {public static voidmain(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 instanceofArithmeticException)

System.out.println("被0除");else{

System.out.println(e.getMessage());

}

}finally{

JOptionPane.showConfirmDialog(null,"OK");

}

}

}

cd3afe35bff8eeb916c30d3cf9da2feb.png

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

2.阅读以下代码(CatchWho.java),写出程序运行结果:

public classCatchWho {public static voidmain(String[] args) {try{try{throw newArrayIndexOutOfBoundsException();

}catch(ArrayIndexOutOfBoundsException e) {

System.out.println("ArrayIndexOutOfBoundsException" + "/内层try-catch");

}throw newArithmeticException();

}catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}catch(ArrayIndexOutOfBoundsException e) {

System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

3f3be38c251c74f35d540c17636dc7e8.png

写出CatchWho2.java程序运行的结果:

public classCatchWho2 {public static voidmain(String[] args) {try{try{throw newArrayIndexOutOfBoundsException();

}catch(ArithmeticException e) {

System.out.println("ArrayIndexOutOfBoundsException" + "/内层try-catch");

}throw newArithmeticException();

}catch(ArithmeticException e) {

System.out.println("发生ArithmeticException");

}catch(ArrayIndexOutOfBoundsException e) {

System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch");

}

}

}

7cf2cf7982a6fd4133d1d9c694f6a8c9.png

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

3.辨析:finally语句块一定会执行吗?

public classSystemExitAndFinally {public static voidmain(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");

}

}

}

bb4bf1f32ca7e993d15098cfd38ec98f.png

因此finally语句不一定执行

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

//UsingExceptions.java//Demonstrating the getMessage and printStackTrace//methods inherited into all exception classes.

public classPrintExceptionStack {public static voidmain( String args[] )

{try{

method1();

}catch( Exception e ) {

System.err.println( e.getMessage()+ "\n");

e.printStackTrace();

}

}public static void method1() throwsException

{

method2();

}public static void method2() throwsException

{

method3();

}public static void method3() throwsException

{throw new Exception( "Exception thrown in method3");

}

}

6515659661a81a8c82b50ac24ee17774.png

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

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

5.成绩:

importjava.util.Scanner;class judgeException extendsException{publicjudgeException(String s){super(s);

}

}class judgeException2 extendsException{publicjudgeException2(String s){super(s);

}

}public classTest {public static voidmain(String[] args) {boolean p=true;while(p){

System.out.println("请输入学生的分数:");

Scanner in=newScanner(System.in);

String s=in.next();try{for(int i=0;i57)){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());

}

}

}

}

2f4c31a9571d91c67a1e04c6570b185f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值