大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说处理Java异常的9个最佳实践[通俗易懂],希望您对编程的造诣更进一步.
Java中的异常处理不是一个简单的主题。初学者发现它很难理解,甚至有经验的开发者也可以花几个小时讨论如何以及应该抛出或处理哪些异常。
1.在finally块中清理资源或使用Try-With-Resource语句
InputStream,之后需要关闭它。这些情况中的一个常见错误是在try块结束时关闭资源。
public void doNotCloseResourceInTry() {
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
// do NOT do this
inputStream.close();
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
使用Finally块
public void closeResourceInFinally() {
FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
} catch (FileNotFoundException e) {
log.error(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e);
}
}
}
}
Java 7的Try-With-Resource
Java异常处理的介绍中对此进行了更详细的解释。
AutoCloseable接口,则可以使用它。这就是大多数Java标准资源所做的事情。当你在try子句中打开资源时,它将在try块执行后自动关闭,或者处理异常。
public void automaticallyCloseResource() {
File file = new File("./tmp.txt");
try (FileInputStream inputStream = new FileInputStream(file);) {
// use the inputStream to read a file
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
2.特定异常
通过额外的检查来避免它。
public void doNotDoThis() throws Exception {
...
}
public void doThis() throws NumberFormatException {
...
}
3.记录你声明的异常
/** * This method does something extremely useful ... * * @param input * @throws MyBusinessException if ... happens */
public void doSomething(String input) throws MyBusinessException {
...
}
4.使用描述信息抛出异常
try {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
}
17:17:26,386 ERROR TestExceptionHandling:52 - java.lang.NumberFormatException:
For input string: "xyz"
5.优先捕获最具体的异常
public void catchMostSpecificExceptionFirst() {
try {
doSomething("A message");
} catch (NumberFormatException e) {
log.error(e);
} catch (IllegalArgumentException e) {
log.error(e)
}
}
6.Don’t Catch Throwable
OutOfMemoryError或
StackOverflowError。两者都是由应用程序无法控制的情况引起的,无法处理。
public void doNotCatchThrowable() {
try {
// do something
} catch (Throwable t) {
// don't do this!
}
}
7.Don’t Ignore Exceptions
public void doNotIgnoreExceptions() {
try {
// do something
} catch (NumberFormatException e) {
// this will never happen
}
}
public void logAnException() {
try {
// do something
} catch (NumberFormatException e) {
log.error("This should never happen: " + e);
}
}
8.Don’t Log and Throw
try {
new Long("xyz");
} catch (NumberFormatException e) {
log.error(e);
throw e;
}
17:44:28,945 ERROR TestExceptionHandling:65 - java.lang.NumberFormatException: For input string: "xyz"
Exception in thread "main" java.lang.NumberFormatException: For input string: "xyz"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.(Long.java:965)
at com.stackify.example.TestExceptionHandling.logAndThrowException(TestExceptionHandling.java:63)
at com.stackify.example.TestExceptionHandling.main(TestExceptionHandling.java:58)
public void wrapException(String input) throws MyBusinessException {
try {
// do something
} catch (NumberFormatException e) {
throw new MyBusinessException("A message that describes the error.", e);
}
}
9.在没有消费的情况下包装异常
public void wrapException(String input) throws MyBusinessException {
try {
// do something
} catch (NumberFormatException e) {
throw new MyBusinessException("A message that describes the error.", e);
}
}
总结
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/13021.html