美文网首页计算机杂谈Java 杂谈
【Java学习笔记】(四)异常处理进阶

【Java学习笔记】(四)异常处理进阶

作者: 胖若两人_ | 来源:发表于2018-04-25 18:26 被阅读2次

这篇文章转载自我的博客

正文之前

上一篇文章已经介绍了异常的基本信息,这次来说一点不一样的:

  1. try-catch-finally语句(其实是上次忘了说)
  2. 自定义异常
  3. 查看异常信息

正文

1. try-catch-finally语句

上一篇文章讲到了try-catch语句捕获异常,在这个语句上有一个延伸,就是finally关键字的使用

finally翻译过来的意思就是"最后",故名思意,finally语句块中的内容最后都会被执行,我们来试一试:

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Test {
    public static void main(String[] args){
        try{
            FileInputStream in = new FileInputStream("test.txt");
        }catch (FileNotFoundException e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println("无论如何我还是会执行的");
            in.close();
        }
    }
}

最后成功打印出了finally语句块中的内容

2. 自定义异常

在我们自定义异常之前,先来看看官方给出的异常类型(JDK 8):

大部分都是没见过的,不要紧,我们可以自己来定义属于自己的异常:

具体的异常都是继承自Exception类,所以我们要自定义的异常也需要继承这个基类:

我们自定义异常,命名MyException,需要继承基类Exception,然后我们调用基类的构造方法,带一个参数:

public class MyException extends Exception {
   public MyException(String message){
       super(message);
   }
}

然后我们测试这个异常,因为是测试,所以直接抛出定义的异常:

public class Test {
    public static void main(String[] args)throws MyException{
        try{
            throw new MyException("自定义异常");
        }catch (MyException e){
            System.out.println(e.getMessage());
        }
    }
}

然后得出结果:

现在项目做的很少,所以自定义异常并没有用过几次,可以参考一下这个项目中定义的用户异常

3. 查看异常信息

有时候我们会需要查看异常信息来进行调试,JDK中给出了13种继承自Throwable基类的方法:

先说说 getMessage() :

the detail message string of this Throwable instance (which may be null)

将Throwable实例(通常所说的异常)的具体信息以String类型给出(可能不存在)

接下来解释另外一个和查看异常信息有关的方法:

printStackTrace()

  1. 前提:

在说着三种之前,需要先提及toString()方法,Throwable类中覆盖了toString()方法:

这里面的意思是:

toString()方法返回一个字符串,代表了Throwable实例(异常)的信息,具体格式为:对象名字: 本地化的信息

我拿上一篇文章中的例子来看:

运行出来的结果第二行:

java.io.FileNotFoundException: test.txt(系统找不到指定的文件。)
at ...

java.io.FileNotFoundException就是Throwable的实例,text.txt...后面的信息就是本地化的信息了,针对不同的位置给出具体的信息,用来调试就能很快进行定位

Exception类中的方法感觉一环扣一环的,接下来会专门写一篇Exception类源码解析
  1. printStackTrace()

这个方法有三种类型:无参,带参数PrintStream和带参数PrintWriter

  • 先说无参,无参的printStackTrace()方法也就是我们平常使用的查看异常信息的方式,直接在控制台打印

  • 带参数PrintStream(打印输出流):能够将信息通过流输出至其他地方,做个测试:

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException{
        OutputStream out;
        try{
            throw new RuntimeException();
        }catch (RuntimeException e){
            out = new FileOutputStream("C:\\Users\\94545\\Desktop\\test.txt");
            //将FileOutputStream作为PrintStream的输出流
            PrintStream printStream = new PrintStream(out);
            e.printStackTrace(printStream);
            out.close();
            printStream.close();
        }
    }
}

成功打印出了信息

  • 带参数PrintWriter(字符打印输出流):将信息输出至其他地方
import java.io.*;

public class Test {
    public static void main(String[] args) throws Exception{
        OutputStream out;
        try{
            throw new RuntimeException();
        }catch (RuntimeException e){
            out = new FileOutputStream("C:\\Users\\94545\\Desktop\\test.txt");
            PrintWriter printWriter = new PrintWriter(out,true);
            e.printStackTrace(printWriter);
            out.close();
            printWriter.close();
        }
    }
}

也成功的打印出了信息

关于其他的继承自Throwable类的方法会专门写一篇源码解读

相关文章

网友评论

    本文标题:【Java学习笔记】(四)异常处理进阶

    本文链接:https://www.haomeiwen.com/subject/ibkelftx.html