java异常

一、继承关系

Throwable 类

  |--Error 类 是系统不可恢复的错误,JVM发生的错误

    | |--OutOfMemoryError 堆内存溢出

    | |--StackOverflowError 栈内存溢出

  |--Exception 类 程序可以检查处理的异常,常见的异常继承根

    |--java.text.ParseException format 解析对象时候发生

    | 如:Date d = dateformat.parse("2010-5-5");

    |--RuntimeException 类 非检查异常,Javac忽略对这类异常的语法检查

      |--IllegalArgumentException |--NullPointerException *

      |--ArrayIndexOutOfBoundsException *

      |--ClassCastException *

      |--NumberFormatException * Integer.parseInt(S)

二、try...catch执行顺序

/**
 * 如果try,catch,finally中都有return,则执行finally中的,
 * 除非System.exit(0)退出程序(System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序)
 * 
 */
public String t2(){
    try{
        return "try";
    }catch(Exception e){
        return "catch";
    }finally {
        return "finally";
    }
}

三、自定义异常

/**
 * 继承Exception、RuntimeException 或Throwable,并重写构造方法即可。
 * 
 */
public class MyException extends Exception {

    public MyException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public MyException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public MyException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
}

 

转载于:https://www.cnblogs.com/lovgge/p/5226828.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值