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

26似乎没有

27.Modify Exercise 3 to convert the exception to a Runtime Exception.


package job;

import java.util.*;

public class Main {

    public static void main(String[] args){

        Main []a=new Main[1];

        try{

            a[1]=new Main();

        }catch (ArrayIndexOutOfBoundsException e){

            throw new RuntimeException(e);

        }

    }

}

output:

Exception in thread "main" java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at job.Main.main(Main.java:18)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at job.Main.main(Main.java:14) 

28.Modify Exercise 4 so that the custom exception class inherits from RuntimeException, and show that the compiler allows you to leave out the try block.


package job;

import java.util.*;

public class Main extends RuntimeException {

    String s;

    Main(String s){
        super(s);

        this.s=s;

        this.show();
    }

    void show(){

        System.out.println("String="+s);

    }

    public static void main(String[] args){

        throw new Main("wrong");

    }

}

output:

String=wrong
Exception in thread "main" job.Main: wrong
    at job.Main.main(Main.java:26) 

29.Modify all the exception types in StormInning.java so that they extend RuntimeException, and show that no exception specifications or try blocks are necessary. Remove the '//!' comments and show how the methods can be compiled without specifications.

package job;
import java.util.*;
class BaseballException extends RuntimeException {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
    public Inning() throws BaseballException {}
    public void event() throws BaseballException {
        // Doesn't actually have to throw anything
    }
    public abstract void atBat() throws Strike, Foul;
    public void walk() {} // Throws no checked exceptions
}
class StormException extends RuntimeException {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}
interface Storm {
    public void event() throws RainedOut;
    public void rainHard() throws RainedOut;
}
public class Main extends Inning implements Storm {
    // OK to add new exceptions for constructors, but you
    // must deal with the base constructor exceptions:\
    public Main()
            throws RainedOut, BaseballException {
    }
    public Main(String s)
            throws Foul, BaseballException {
    }
    // Regular methods must conform to base class:
    public void walk() throws PopFoul {
    } //Compile error
    // Interface CANNOT add exceptions to existing
    // methods from the base class:
    public void event() throws RainedOut {
    }
    // If the method doesn't already exist in the
    // base class, the exception is OK:
    public void rainHard() throws RainedOut {
    }

    // You can choose to not throw any exceptions,

    // even if the base version does:

//  public void event() {}

    // Overridden methods can throw inherited exceptions:

    public void atBat() throws PopFoul {
    }

    public static void main(String[] args) {

        try {

            Main si = new Main();

            si.atBat();

        } catch (PopFoul e) {

            System.out.println("Pop foul");

        } catch (RainedOut e) {

            System.out.println("Rained out");

        } catch (BaseballException e) {

            System.out.println("Generic baseball exception");

        }

        // Strike not thrown in derived version.

        try {

            // What happens if you upcast?

            Inning i = new Main();

            i.atBat();

            // You must catch the exceptions from the

            // base-class version of the method:

        } catch (Strike e) {

            System.out.println("Strike");

        } catch (Foul e) {

            System.out.println("Foul");

        } catch (RainedOut e) {

            System.out.println("Rained out");

        } catch (BaseballException e) {

            System.out.println("Generic baseball exception");

        }
    }
}

30.Modify Human.java so that the exceptions inherit from RuntimeException. Modify main() so that the technique in TurnOffChecking.java is used to handle the different types of exceptions.

package job;
import java.util.*;

class SomeOtherException extends Exception{}

class Annoyance extends RuntimeException {}

class Sneeze extends Annoyance {}

class WrapCheckedException {

    void throwRuntimeException(int type) {

        try {

            switch(type) {

                case 0: throw new Sneeze();

                case 1: throw new Annoyance();

                case 2: throw new RuntimeException("Where am I?");

                default: return;

            }

        } catch(Exception e) { // Adapt to unchecked:

            throw new RuntimeException(e);

        }

    }

}

public class Main {

    public static void main(String[] args) {

        // Catch the exact type:

        WrapCheckedException wc = new WrapCheckedException();

        for (int i = 0; i < 4; i++) {

            try {

                if (i < 3) wc.throwRuntimeException(i);

                else throw new SomeOtherException();

            } catch (SomeOtherException s) {

                s.printStackTrace();

            } catch (RuntimeException r) {


                try {

                    throw r.getCause();

                } catch (Sneeze s) {

                    s.printStackTrace();

                } catch (Annoyance a) {

                    a.printStackTrace();

                } catch (Throwable e) {

                    e.printStackTrace();

                }

            }

        }

    }
}

output:

job.Sneeze
    at job.WrapCheckedException.throwRuntimeException(Main.java:18)
    at job.Main.main(Main.java:50)
job.Annoyance
    at job.WrapCheckedException.throwRuntimeException(Main.java:20)
    at job.Main.main(Main.java:50)
java.lang.RuntimeException: Where am I?
    at job.WrapCheckedException.throwRuntimeException(Main.java:22)
    at job.Main.main(Main.java:50)
job.SomeOtherException
    at job.Main.main(Main.java:52)

结束~
我的另一个博客:https://www.cnblogs.com/hsjj/
会不定时的更新算法题
有问题欢迎发送邮件至hpzhangjunjiell@163.com
我们下次见哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值