Java:异常

详细总结一下个人学习java异常的相关知识点

java中具备处理错误的机制,其中针对的对象称为异常——Exception :当出现异常时,如果没有处理措施会出现编译出错,程序中断等问题

    首先罗列一些常见的异常:
    NullPointerException :空指针异常
    NumberFormatException :数字转字符异常
    ClassNotFoundException :无法找到指定类异常
    IndexOutOfBoundsException :数组下标越界异常
    FileNotFoundException :无法找到文件异常
    OutOfMemoryException :内存不足异常
    SQLException :数据库操作异常
    IOException :输入输出异常

一、异常的处理

    不同类型的异常都是一个继承自Exception类的一个子类,当运行一段代码可能出现这些种类的异常时,就需要考虑到这种可能性并及时处理。

1. try...catch

    使用try...catchtry使程序试图执行一段代码,而catch则负责捕捉可能产生的异常(对应的异常类),一旦异常出现,就能被处理。

public static void main(String[] args) 
{    
	try{
		insert();
	}catch(Exception e){
	        e.printStackTrace();    
	}finally{
		System.out.println("Apple Pen~");
	}
}

2.finally

    与try...catch连用,“最终的”,也就是说,不管try..catch执行情况如何,异常是否被捕捉,即使catch的异常处理语句组中含有return,最后都要执行finally的异常处理语句组。(代码如上)

3.throws , throw

    使用try..catch相当于直接将try的代码块中可能发生的异常直接捕获。但如果一个程序有很多函数层,那么想让每一个异常都在当前层处理显然会出现代码的赘余。例如:main函数同时调用函数A,B,C,D,而A,B,C,D中都可能产生同样的异常,分别对A,B,C,D使用try...catch显然不是好的选择。
    于是引入了throwsthrow,“抛出”,若不想在当前函数层捕捉异常,可以选择将异常抛出,交给调用该函数层的上一级函数处理 ; 上一级也同样可以使用throwsthrow抛出,抑或是直接捕捉处理掉。
throws :表示当前一场语句组可能抛出异常。
throw : 表示抛出了某个异常。

@Override
public void insert(String s) throws IndexIsOutofRangeException,IndexIsNagetiveException{
    insert(length,s);
}
@Override
public void insert(int pos, String s) throws IndexIsNagetiveException, IndexIsOutofRangeException {
    if(pos<0){
        throw new IndexIsNagetiveException();
    }
    if(pos>this.length){
        throw new IndexIsOutofRangeException();
    }
    char[] add = s.toCharArray();
    int l = s.length();
    length = length + l;
    char[] newvalue = new char[length];
    System.arraycopy(value,0,newvalue,0,pos);
    System.arraycopy(add,0,newvalue,pos,l);
    System.arraycopy(value,pos,newvalue,pos+l,length-pos-l);
    value = newvalue;
}
public static void main(String[] args){
    try{
        MyStringBuffer ms1 = new MyStringBuffer("(Nobody knows better than me!!)");
        MyStringBuffer ms2 = new MyStringBuffer("I knows economy,polity and life.");
        ms1.insert("—— says Trump.");
        System.out.println(ms1);
        ms2.insert(17,",military ");
        System.out.println(ms2);
        ms2.insert(-1,"America");
        System.out.println(ms2);
        ms2.insert(100,"China");
        System.out.println(ms2);
        ms2.delet(-1);
        System.out.println(ms2);
        ms2.delet(5,4);
        System.out.println(ms2);
    }catch(IndexIsOutofRangeException e){
        e.printStackTrace();
    }catch(IndexIsNagetiveException e){
        e.printStackTrace();
    }
}

上述代码中,构造了两个不同参数形式的insert()函数,异常的处理过程:
main函数调用ms2.insert(-1,"America");
②含两个参数的insert(int pos,String s)可能会抛出异常(throws IndexIsNagetiveException, IndexIsOutofRangeException)
③控制语句判断执了throw new IndexIsNagetiveException();抛出异常
④返回到调用insert(int pos,String s)main函数,异常被try...catch捕获

二、异常类解析

图片来源http://www.pianshen.com/article/9702219614/

我们所说的异常类ExceptionThrowable的子类,除了异常还有错误类Error:其中Error是程序不可查的错误,属于JVM运行错误,不需要去进行try...catch处理。

1.Runtimeexception , Checkedexception

    异常向下也有很多子类,可分为Runtimeexception 和 Checkedexception
Runtimeexception:运行时异常——如空指针异常,数组下标越界异常等。

这些异常的特点是程序员测试运行时出现的异常,属于程序员可调控范围内的异常,这种异常是不需要必须声明可能抛出异常或者捕获处理的。

Checkedexception:可查异常——如查找不到指定文件异常,数据库操作异常等。

这些异常的特点是属于用户可查的异常,比如因为用户做了文件位置的移动导致程序无法正常运行,这种异常是程序员可调控范围之外的,一定要声明可能抛出异常或者捕获处理。

2.自定义异常

    所有异常都继承自Exception类,自定义异常同理。

package CustomizeException;

public class IndexIsNagetiveException extends Exception {
    public IndexIsNagetiveException(){
    }
    public IndexIsNagetiveException(String message){
        super(message);
    }
    public IndexIsNagetiveException(String message,Exception cause){
        super(message,cause);
    }
}

Exception类有:

三种构造方法

public Exception()
public Exception(String message)
public Exception(String message,Throwable cause)

三种方法

getMessage()
getCause()
printStackTrace()

message相当于该异常的信息。
cause是一个Throwable类,它的含义是引发当前异常的底层原因,也就是说,可能存在一个异常的影响,导致了另一个异常的情况,这里返回的信息就是“原因”cause

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值