2019-06-03-toString()直接输出

2019-06-03  本文已影响0人  陈林晖
package test;

class MyException extends Exception {
    String id;

    public MyException(String str) {
        id = str;
    }

    public String toString() {//toString()方法
        return "我是异常:" + id;
    }
}

package test;

public class TestException {
    public static void main(String a[]) {
        try {
            throw new MyException("一个测试异常");
        } catch (MyException e) {
            System.out.println(e);//默认调用toString()--e.toString()--
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读