JAVA编程思想第十二章练习题——异常

练习1:

public class E01 {
    public static void main(String[] args) {
        try {
            throw new Exception("An exception in main");
        }catch (Exception e){
            System.out.println("e = " + e.getMessage());
        }finally {
            System.out.println("Finally!");
        }
    }
}

练习2:

public class E02 {
    public static void main(String[] args) {
        String s = null;
        try {
            s.toString();
        }catch (Exception e){
            System.out.println("Caught exception : " + e);
        }
    }
}

 练习3:

public class E03 {
    public static void main(String[] args) {
        char[] array = new char[10];
        try {
            array[10] = 'x';
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("e = " + e);
        }
    }
}

练习4:

class MyException1 extends Exception{
    String msg;

    public MyException1(String msg) {
        this.msg = msg;
    }
    public void printMsg(){
        System.out.println("msg = " + msg);
    }
}

class MyException3 extends Exception{
    public MyException3(String s) {super(s);
    }
}

public class E04 {
    public static void main(String[] args) {
        try {
            throw new MyException1("MyException message");
        }catch (MyException1 e){
            e.printMsg();
        }
        try {
            throw new MyException3("MyException3 message");
        }catch (MyException3 e){
            System.out.println("e = " + e.getMessage());
        }
    }
}

练习5:

class ResumerException extends Exception{}

class Resumer{
    static int count = 3;
    static void f() throws ResumerException{
        if(--count > 0 )
            throw new ResumerException();
    }
}
public class E05 {
    public static void main(String[] args) {
        while (true){
            try {
                Resumer.f();
            }catch (ResumerException e){
                System.out.println("e = " + e);
                continue;
            }
            System.out.println("Go through...");
            break;
        }
        System.out.println("Successful!");

    }
}

import java.util.logging.*;
import java.io.*;

class LoggingException1 extends Exception{
    private static Logger logger = Logger.getLogger("LoggingException1");
    public LoggingException1(){
        StringWriter trace = new StringWriter();
        printStackTrace(new PrintWriter(trace));
        logger.severe(trace.toString());
    }
}

class LoggingException2 extends Exception{
    private static Logger logger = Logger.getLogger("LoggingException2");
    public LoggingException2(){
        StringWriter trace = new StringWriter();
        printStackTrace(new PrintWriter(trace));
        logger.severe(trace.toString());
    }
}

public class E06 {
    public static void main(String[] args) {
        try {
            throw new LoggingException1();
        }catch (LoggingException1 e){
            System.out.println("e = " + e);
        }
        try {
            throw new LoggingException2();
        }catch (LoggingException2 e){
            System.out.println("e = " + e);
        }
    }

}

 

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

public class E03 {
    private static Logger logger = Logger.getLogger("E07");
    static void logException(Exception e){
        StringWriter trace = new StringWriter();
        e.printStackTrace(new PrintWriter(trace));
        logger.severe(trace.toString());
    }
    public static void main(String[] args) {
        char[] array = new char[10];
        try {
            array[10] = 'x';
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("e = " + e);
            logException(e);
        }
    }
}

class Thrower{
    public void f(){}
    public void g() throws MyException1{
        throw new MyException1("Inside f()");
    }
}
public class E08 {
    public static void main(String[] args) {
        Thrower t = new Thrower();
        try {
            t.g();
        }catch (MyException1 e){
            e.printMsg();
        }
    }

}

class ExBase extends Exception{}
class Ex1 extends ExBase{}
class Ex2 extends ExBase{}
class Ex3 extends ExBase{}

class Thrower2{
    void f() throws Ex1,Ex2,Ex3{
        throw new Ex1();
    }
}
public class E09 {
    public static void main(String[] args) {
        Thrower2 t = new Thrower2();
        try {
            t.f();
        }catch (Ex1 e){
            System.out.println("e = " + e);
        }catch (ExBase e){
            System.out.println("e = " + e);
        }catch (Exception e){
            System.out.println("e = " + e);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值