异常
异常:是程序执行中的非正常事件,程序无法再按预想的流程执行。
可以将错误信息传递给上层调用者,并报告“案发现场”的信息。
也是return之外的第二种退出方式
在JAVA中,用类的形式对不正常情况进行了描述和封装对象。当程序出现问题时,调用相应的处理办法。
描述不正常情况的类,就称为异常类。将流程代码和异常代码进行分离。
异常就是JAVA通过面向对象的思想,将问题封装成了对象。用异常类对其进行描述。不同的问题,用不同的类进行描述。那么意味着,问题有多少,类就有多少。
异常的分类
问题很多,意味着描述的类也很多,将其共性进行向上抽取,就形成了异常体系。最终异常分为两大类:
Throwable(父类):问题发生,就应该抛出,让调用者处理。该体系的特点就在于Throwable及其子类都具有可抛性。
两个关键字实现可抛性:throws、throw
1.一般不可处理的。Error(错误)
特点:是由JVM(java虚拟机)抛出的严重性的问题。这种问题发生,一般不针对性处理,直接修改程序。
2.可以处理的。Exception(异常)
特点:子类的后缀名都是用其父类名作为后缀,阅读性很强。
而Exception中,还分为两类,一类是RuntimeException,一类是其他Exception。而RuntimeException和Error一起构成了
Unchecked Exception,剩下的Exception成为CheckedException。
异常抛出throw
public static void main(String [] args){
...
throw new 异常方法();
...
}
throw为关键字,专门用于抛出异常动作。
可以看出,异常时,底层throw直接调用异常方法,抛出异常,只不过这些都在底层完成,我们看不到而已。
JAVA虚拟机它有一套异常处理机制,就是会把异常的各种信息,位置等报出来,以供解决异常。
异常-自定义异常&异常类的抛出throws
/**
* Use this Exception class to show that file input String
* @author Raymo
*
*/
public class StringNotFitException extends Exception {
/**
* Constructs a new exception with null as its detail message.
*/
public StringNotFitException() {
super();
}
/**
* Constructs a new exception with the specified detail message.
* @param message the detail message. The detail message is saved
* for later retrieval by the Throwable.getMessage() method.
*/
public StringNotFitException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and cause.
* Note that the detail message associated with cause is not
* automatically incorporated in this exception's detail message.
* @param message the detail message (which is saved for later
* retrieval by the Throwable.getMessage() method).
* @param cause the cause (which is saved for later retrieval by the
* Throwable.getCause() method). (A null value is permitted,
* and indicates that the cause is nonexistent or unknown.)
*/
public StringNotFitException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail message of
* (cause==null ? null : cause.toString()) (which typically contains the class
* and detail message of cause). This constructor is useful for exceptions that
* are little more than wrappers for other throwables (for example,
* PrivilegedActionException).
*
*
* @param cause the cause (which is saved for later retrieval by the
* Throwable.getCause() method). (A null value is permitted, and indicates that
* the cause is nonexistent or unknown.)
*/
public StringNotFitException(Throwable cause) {
super(cause);
}
}
以上是一个自定义的Java异常类示例,根据自己的需要,一般是继承自Exception类,然后构造函数直接调用父类的构造函数即可。
try-catch-finally
try{
....
}catch
{
return; 退出整个方法,但finally依旧执行。
System.exit(0);退出java虚拟机,只有这种情况finally不会执行。
}
finally{ 通常用于关闭(释放)资源
除退出虚拟机一种情况外,无论怎样,都会执行。
}
try-catch-finally代码块组合特点:
1.try-catch-finally常见组合体
2.try-catch(可以多个catch)没有finally,没有资源需要释放(关闭),可以不用finally。
3.try-finally,没有catch时,方法旁边需要throws声明,因为没catch没处理。异常无法直接catch处理,但是资源需要关闭,这时用此组合。
注意事项
根据LSP,在子类继承父类时,异常要协变!!!
1.子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的子类。
2.如果父类抛出多个异常,那么子类只能抛出父类异常的子集。----子类覆盖父类只能抛出父类异常或者子类或者子集。如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,只能try。