章节练习:异常处理

代码阅读题

第1题:

/**
 * @author yhm
 * @create 2020-07-16 11:09
 */
public class Test01 {
    public static int test(int x,int y){
        int result = x;
        try {
            if (x < 0 || y < 0){
                return 0;
            }
            result = x + y;
            return result;
        }finally {
            result = x - y;
        }
    }

    public static void main(String[] args) {
        int test = test(3,5);
        System.out.println(test);
    }
}

/*
* (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
* (2)如果finally中有return语句,那么try...catch...finally结构
* 一定从finally中的return回去
* (3)如果finally中没有return语句,那么try...catch...finally结构
* 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值
*/

第2题

/**
 * @author yhm
 * @create 2020-07-16 11:22
 */
public class Test02 {
    public static void main(String[] args) {
        try {
            return;
        }finally {
            System.out.println("finally");
        }
    }
}

/*
* finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
*/

第3题

/**
 * @author yhm
 * @create 2020-07-16 11:23
 */
public class Test03 {
    {
        System.out.println("a");
    }
    static {
        System.out.println("b");
    }
    Test03(){
        System.out.println("c");
    }
    public static String getOut(){
        try {
            return "1";
        }catch (Exception e){
            return "2";
        }finally {
            return "3";
        }
    }

    public static void main(String[] args) {
        System.out.println(getOut());
    }
}

/*
* (1)main()执行之前,需要先完成所在类的初始化
* (2)类初始化由两部分代码组成:①静态变量的显式赋值代码②静态代码块
* (3)没有创建对象,不会执行实例初始化相关的代码
* (4)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
* (5)如果finally中有return语句,那么try...catch...finally结构
* 一定从finally中的return回去
*/

第4题

/**
 * @author yhm
 * @create 2020-07-16 11:28
 */
public class Test04 {
    static int i = 0;

    public static void main(String[] args) {
        System.out.println(test());
    }

    public static int test(){
        try {
            return ++i;
        }finally {
            return ++i;
        }
    }
}

/*
* (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
* (2)如果finally中有return语句,那么try...catch...finally结构
* 一定从finally中的return回去,但是try和catch中的return语句中返回值的表达式会执行。
* (3)如果finally中没有return语句,那么try...catch...finally结构
* 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值
*/

第5题

import java.io.IOException;

/**
 * @author yhm
 * @create 2020-07-16 11:30
 */
public class Test05 {
    public static void main(String[] args) {
        int a = -1;
        try {
            if (a > 0){
                throw new RuntimeException("");
            }else if (a < 0){
                throw new IOException("");
            }else return;
        }
        catch (IOException ioe){
            System.out.println("IOException");
        }catch (Throwable e){
            System.out.println("Throwable");
        }finally {
            System.out.println("finally");
        }
    }
}

/*
* (1)throw用于手动抛出异常
* (2)无论是JVM抛出的异常还是throw抛出的异常都需要catch处理
* (3)catch是多分支结构,从上到下按顺序匹配,只会进入第一个匹配上的catch分支
* (4)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
*/

第6题

/**
 * @author yhm
 * @create 2020-07-16 11:34
 */
public class Test06 {
    public static int fun(){
        int result = 5;
        try {
            result = result / 0;
            return result;
        }catch (Exception e){
            System.out.println("Exception");
            result = -1;
            return result;
        }finally {
            result = 10;
            System.out.println("I am in finally");
        }
    }

    public static void main(String[] args) {
        int x = fun();
        System.out.println(x);
    }
}

/*
* (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
* (2)如果finally中有return语句,那么try...catch...finally结构
* 一定从finally中的return回去
* (3)如果finally中没有return语句,那么try...catch...finally结构
* 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值
*/

第7题

/**
 * @author yhm
 * @create 2020-07-16 11:36
 */
public class Test07 {
    public static int aMethod(int i)throws Exception{
        try {
            return i / 10;
        }catch (Exception ex){
            throw new Exception("exception in aMethod");
        }finally {
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        try {
            aMethod(0);
        }catch (Exception e){
            System.out.println("exception in main");
        }
    }
}
/*
* (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,
* 也不管try和catch中是否有return语句,都会执行的部分
* (2)throw用于手动抛出异常
* (3)throws用于将异常抛给调用者处理
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值