异常
异常也是java中的类
所有异常的父类是:Throwable
异常分为两种
- Error :系统级别的错误,我们处理不了,不需要关心
- Exception:因为应用程序导致的异常,这个是可以处理的
异常按照处理方式进行分类
- checked exception:语法要求必须使用try catch 或者throws语句处理的异常
- unchecked exception: 语法不一定要求用try catch或者throws语句处理的异常
unchecked exception
就是Error和RuntimeException都是 unchecked exception异常,除了这两个其他都是 checked exception。(runtimeException 继承自exception)
如何在代码中定义一个异常并且抛出去
public void functiona() throws Exception{
throw new Exception("")
}
在抽象方法中抛出异常的情况
抽象方法中声明的异常是接口方法签名的一部分
- 接口中声明了抛出异常,实现类中可以抛,也可以不抛。抛的时候必须是接口声明的类或其子类
- 接口中没有声明抛出异常,实现类中可以抛RuntionException,也可以不抛。如果抛checked exception就会出错。(可以选择catch住checked exception ,然后将它封在RuntimeException里)
try catch finally
- finally语句会在方法返回后(return语句后),后面的方法开始前 执行
- 无论因为return结束还是异常结束,finally语句都会执行
- finally语句里面最好不要有return语句,因为他会导致try和catch里面的return语句失效
- finally语句里面给return的变量赋值没用
try with resources
在 JDK 7 之前,资源需要手动关闭。
例如下面一个很常见的文件操作的例子:
Charset charset = Charset.forName("US-ASCII");
String s = ...;
BufferedWriter writer = null;
try {
writer = Files.newBufferedWriter(file, charset);
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
} finally {
if (writer != null) writer.close();
}
在 JDK 7 之前,你一定要牢记在 finally 中执行 close 以释放资源
try-with-resources 是 JDK 7 中一个新的异常处理机制,它能够很容易地关闭在 try-catch 语句块中使用的资源。所谓的资源(resource)是指在程序完成后,必须关闭的对象。try-with-resources 语句确保了每个资源在语句结束时关闭。所有实现了 java.lang.AutoCloseable 接口(其中,它包括实现了 java.io.Closeable 的所有对象),可以使用作为资源。
例如,我们自定义一个资源类
public class Demo {
public static void main(String[] args) {
try(Resource res = new Resource()) {
res.doSome();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class Resource implements AutoCloseable {
void doSome() {
System.out.println("do something");
}
@Override
public void close() throws Exception {
System.out.println("resource is closed");
}
}
执行输出如下。可以看到,资源终止被自动关闭了。
do something
resource is closed
再来看一个例子,是同时关闭多个资源的情况:
public class Main2 {
public static void main(String[] args) {
try(ResourceSome some = new ResourceSome();
ResourceOther other = new ResourceOther()) {
some.doSome();
other.doOther();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class ResourceSome implements AutoCloseable {
void doSome() {
System.out.println("do something");
}
@Override
public void close() throws Exception {
System.out.println("some resource is closed");
}
}
class ResourceOther implements AutoCloseable {
void doOther() {
System.out.println("do other things");
}
@Override
public void close() throws Exception {
System.out.println("other resource is closed");
}
}
最终输出为:
do something
do other things
other resource is closed
some resource is closed
在 try 语句中越是最后使用的资源,越是最早被关闭。
自定义异常
继承Exception类,实现里面的构造方法
public class MyException extends Exception{
public MyException(){
}
public MyException(String massage){
super(message);
}
//massage:是描述异常的信息
//Throwable:造成这个异常的异常(也就是谁引起了我这个异常)
public MyException(String massage, Throwable cause){
super(message,cause);
}
}
使用
try{
}catch(Exception ex){
throw new MyException("",ex);
}
异常不能当做程序跳转
异常代表程序出错,不要使用异常来做正常的程序跳转。因为异常的创建和处理都很耗费资源
错误的示例:使用”凌波微步“ 跨越几个调用方法,跳转到catch语句。