Java编程思想第四版第十二章习题(中)

这篇博客展示了Java编程中异常处理的实践,包括自定义异常类型、捕获并重新抛出异常、try-catch-finally语句的使用,以及在构造器中处理异常的情况。通过一系列的代码示例,解释了如何在遇到异常时进行适当的错误处理,并演示了异常层级结构的使用。
摘要由CSDN通过智能技术生成

10.Create a class with two methods, f() and g(). In g(), throw an exception of a new type that you define. In f(), call g(), catch its exception and, in the catch clause, throw a different exception (of a second type that you define). Test your code in main().

package job;
import java.util.*;
public class Main {
    static void f() throws Exception {
        try {
            g();
        }catch (Exception e){
            e.printStackTrace(System.out);
            System.out.println("throw from f()");
            throw new Exception("way of f()");
        }
    }
    static void g()throws Exception{
        System.out.println("throw from g()");
        throw new Exception("way of g()");
    }
    public static void main(String[] args) {
        try {
            f();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}

 output:

throw from g()
java.lang.Exception: way of g()
    at job.Main.g(Main.java:15)
    at job.Main.f(Main.java:6)
    at job.Main.main(Main.java:19)
throw from f()
java.lang.Exception: way of f()
    at job.Main.f(Main.java:10)
    at job.Main.main(Main.java:19)

11.Repeat the previous exercise, but inside the catch clause, wrap g()'s exception in a RuntimeException.

package job;
import java.util.*;
public class Main {
    static void f() throws Exception {
        try {
            g();
        }catch (RuntimeException e){
            e.printStackTrace(System.out);
            System.out.println("throw from f()");
            throw new Exception("way of f()");
        }
    }
    static void g()throws RuntimeException{
        System.out.println("throw from g()");
        throw new RuntimeException("way of g()");
    }
    public static void main(String[] args) {
        try {
            f();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}

output:

throw from g()
java.lang.RuntimeException: way of g()
    at job.Main.g(Main.java:15)
    at job.Main.f(Main.java:6)
    at job.Main.main(Main.java:19)
throw from f()
java.lang.Exception: way of f()
    at job.Main.f(Main.java:10)
    at job.Main.main(Main.java:19)

12.Modify innerclasses/Sequence.java so that it throws an appropriateexception if you try to put in too many elements.

package job;
import java.util.*;

interface Selector {
    boolean end();
    Object current();
    void next();
}

public class Main {
    private Object[] items;
    private int next = 0;

    public Main(int size) {
        items = new Object[size];
    }

    public void add(Object x) throws Exception {
        if (next <= items.length)
            items[next++] = x;
        if (next > items.length) {
            throw new Exception();
        }
    }

    private class SequenceSelector implements Selector {
        private int i = 0;

        public boolean end() {
            return i == items.length;
        }

        public Object current() {
            return items[i];
        }

        public void next() {
            if (i < items.length)
                i++;
        }
    }

    public Selector selector() {
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        try {
            Main sequence = new Main(10);
            for (int i = 0; i < 11; i++) {
                sequence.add(i);
            }
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值