你有没有这样的印象,当你想要更新一款 APP 的时候,它的更新日志里总有这么一两句描述:java
做为一名负责任的程序员,咱们固然但愿程序不会出现 bug,由于 bug 出现的越多,间接地证实了咱们的编程能力越差,至少领导是这么看的。程序员
事实上,领导是不会拿本身的脑壳宣言的:“咱们的程序毫不存在任何一个 bug。”但当程序出现 bug 的时候,领导会坚决果断地选择让程序员背锅。数据库
为了让本身少背锅,咱们能够这样作:编程
还有一点须要作的是,在敲代码以前,学习必要的编程常识,作到兵马未动,粮草先行。性能
在 Java 中,异常(Throwable)的层次结构大体以下。学习
Error 类异常描述了 Java 运行时系统的内部错误,好比最多见的 OutOfMemoryError
和 NoClassDefFoundError
。测试
致使 OutOfMemoryError
的常见缘由有如下几种:编码
OutOfMemoryError
的解决办法须要视状况而定,但问题的根源在于程序的设计不够合理,须要经过一些性能检测才能找得出引起问题的根源。spa
致使 NoClassDefFoundError
的缘由只有一个,Java 虚拟机在编译时能找到类,而在运行时却找不到。设计
NoClassDefFoundError
的解决办法,我截了一张图,如上所示。当一个项目引用了另一个项目时,切记这一步!
Exception(例外)一般可分为两类,一类是写代码的人形成的,好比访问空指针(NullPointerException
)。应当在敲代码的时候进行检查,以杜绝这类异常的发生。
if (str == null || "".eqauls(str)) {
}
复制代码
另一类异常不是写代码的人形成的,要么须要抛出,要么须要捕获,好比说常见的 IOException
。
抛出的示例。
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("沉默王二.txt");
int b;
while ((b = is.read()) != -1) {
}
}
复制代码
捕获的示例。
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("沉默王二.txt");
int b;
while((b = is.read()) != -1) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
当抛出异常的时候,剩余的代码就会终止执行,这时候一些资源就须要主动回收。Java 的解决方案就是 finally
子句——无论异常有没有被捕获,finally
子句里的代码都会执行。
在下面的示例当中,输入流将会被关闭,以释放资源。
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("沉默王二.txt");
int b;
while ((b = is.read()) != -1) {}
} catch (IOException e) {
e.printStackTrace();
} finally {
is.close();
}
}
复制代码
但我总以为这样的设计有点问题,由于 close()
方法一样会抛出 IOException
:
public void close() throws IOException {}
复制代码
也就是说,调用 close()
的 main 方法要么须要抛出 IOException
,要么须要在 finally
子句里从新捕获 IOException
。
选择前一种就会让 try catch
略显尴尬,就像下面这样。
public static void main(String[] args) throws IOException {
InputStream is = null;
try {
is = new FileInputStream("沉默王二.txt");
int b;
while ((b = is.read()) != -1) {}
} catch (IOException e) {
e.printStackTrace();
} finally {
is.close();
}
}
复制代码
选择后一种会让代码看起来很臃肿,就像下面这样。
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("沉默王二.txt");
int b;
while ((b = is.read()) != -1) {}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制代码
总之,咱们须要另一种更优雅的解决方案。JDK7 新增了 Try-With-Resource
语法:若是一个类(好比 InputStream
)实现了 AutoCloseable
接口,那么就能够将该类的对象建立在 try
关键字后面的括号中,当 try-catch
代码块执行完毕后,Java 会确保该对象的 close
方法被调用。示例以下。
public static void main(String[] args) {
try (InputStream is = new FileInputStream("沉默王二.txt")) {
int b;
while ((b = is.read()) != -1) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
关于异常处理机制的使用,我这里总结了一些很是实用的建议,但愿你可以采纳。
1)尽可能捕获原始的异常。
实际应该捕获 FileNotFoundException
,却捕获了泛化的 Exception
。示例以下。
InputStream is = null;
try {
is = new FileInputStream("沉默王二.txt");
} catch (Exception e) {
e.printStackTrace();
}
复制代码
这样作的坏处显而易见:假如你喊“王二”,那么我就敢答应;假如你喊“老王”,那么我还真不敢答应,万一你喊的我妹妹“王三”呢?
不少初学者误觉得捕获泛化的 Exception
更省事,但也更容易让人“丈二和尚摸不着头脑”。相反,捕获原始的异常可以让协做者更轻松地辨识异常类型,更容易找出问题的根源。
2)尽可能不要打印堆栈后再抛出异常
当异常发生时打印它,而后从新抛出它,以便调用者可以适当地处理它。就像下面这段代码同样。
public static void main(String[] args) throws IOException {
try (InputStream is = new FileInputStream("沉默王二.txt")) {
}catch (IOException e) {
e.printStackTrace();
throw e;
}
}
复制代码
这彷佛考虑得很周全,可是这样作的坏处是调用者可能也打印了异常,重复的打印信息会增添排查问题的难度。
java.io.FileNotFoundException: 沉默王二.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at learning.Test.main(Test.java:10)
Exception in thread "main" java.io.FileNotFoundException: 沉默王二.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at learning.Test.main(Test.java:10)
复制代码
3)千万不要用异常处理机制代替判断
我曾见过相似下面这样奇葩的代码,原本应该判 null
的,结果使用了异常处理机制来代替。
public static void main(String[] args) {
try {
String str = null;
String[] strs = str.split(",");
} catch (NullPointerException e) {
e.printStackTrace();
}
}
复制代码
捕获异常相对判断花费的时间要多得多!咱们能够模拟两个代码片断来对比一下。
代码片断 A:
long a = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
try {
String str = null;
String[] strs = str.split(",");
} catch (NullPointerException e) {
}
}
long b = System.currentTimeMillis();
System.out.println(b - a);
复制代码
代码片断 B:
long a = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
String str = null;
if (str != null) {
String[] strs = str.split(",");
}
}
long b = System.currentTimeMillis();
System.out.println(b - a);
复制代码
100000 万次的循环,代码片断 A(异常处理机制)执行的时间大概须要 1983 毫秒;代码片断 B(正常判断)执行的时间大概只须要 1 毫秒。这样的比较虽然不够精确,但足以说明问题。
4)不要盲目地过早捕获异常
若是盲目地过早捕获异常的话,一般会致使更严重的错误和其余异常。请看下面的例子。
InputStream is = null;
try {
is = new FileInputStream("沉默王二.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int b;
try {
while ((b = is.read()) != -1) {
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
假如文件没有找到的话,InputStream
的对象引用 is 就为 null
,新的 NullPointerException
就会出现。
java.io.FileNotFoundException: 沉默王二.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at learning.Test.main(Test.java:12)
Exception in thread "main" java.lang.NullPointerException
at learning.Test.main(Test.java:28)
复制代码
NullPointerException
并非程序出现问题的本因,但实际上它出现了,无形当中干扰了咱们的视线。正确的作法是延迟捕获异常,让程序在第一个异常捕获后就终止执行。
好了,关于异常咱们就说到这。异常处理是程序开发中必不可少的操做之一,但如何正确优雅地对异常进行处理倒是一门学问,好的异常处理机制能够确保程序的健壮性,提升系统的可用率。