详细总结一下个人学习java异常的相关知识点
java中具备处理错误的机制,其中针对的对象称为异常——Exception :当出现异常时,如果没有处理措施会出现编译出错,程序中断等问题
首先罗列一些常见的异常:
NullPointerException :空指针异常
NumberFormatException :数字转字符异常
ClassNotFoundException :无法找到指定类异常
IndexOutOfBoundsException :数组下标越界异常
FileNotFoundException :无法找到文件异常
OutOfMemoryException :内存不足异常
SQLException :数据库操作异常
IOException :输入输出异常
一、异常的处理
不同类型的异常都是一个继承自Exception类的一个子类,当运行一段代码可能出现这些种类的异常时,就需要考虑到这种可能性并及时处理。
1. try...catch
使用try...catch
,try
使程序试图执行一段代码,而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
显然不是好的选择。
于是引入了throws
和throw
,“抛出”,若不想在当前函数层捕捉异常,可以选择将异常抛出,交给调用该函数层的上一级函数处理 ; 上一级也同样可以使用throws
和throw
抛出,抑或是直接捕捉处理掉。
①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/
我们所说的异常类Exception
是Throwable
的子类,除了异常还有错误类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
。