项目中捕获异常的优化
2015-01-16 10:07
260 查看
</pre>1、不用在循环中使用try()catch(); 每次捕获都需要消耗时间的</p><p></p><p>2、尽量捕获具体的异常类,不要用catch(Exception e)去替代所有的异常,因为jvm识别具体的异常是需要消耗时间的。</p><p></p><p><pre name="code" class="html">package com.team.gaoguangjin.javabase.exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import lombok.extern.slf4j.Slf4j; /** * * @author gaoguangjin 为什么不提倡catch(Exception) try..catch 在出现异常的时候影响性能; 应该捕获更具体得异常,比如IOExeception,OutOfMemoryException等 */ @Slf4j public class ExceptionCapability { public static void main(String[] args) { exceptionByListTest(); exectionCapabilityTest(); } /** * 测试循环里面添加异常捕获 */ private static void exceptionByListTest() { long time = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { catchExceptionTest(); } long endTime = System.currentTimeMillis(); long time2 = System.currentTimeMillis(); try { for (int i = 0; i < 100; i++) { catchExceptionNormal(); } } catch (Exception e) { log.error("捕获Exception:" + e.getLocalizedMessage()); } long endTime2 = System.currentTimeMillis(); log.info("catchExceptionTest花费时间:" + (endTime - time)); log.info("catchNormallException花费时间:" + (endTime2 - time2)); } private static void catchExceptionNormal() throws IOException { File file = new File("aa"); FileInputStream fis = new FileInputStream(file); fis.close(); } /** * 比较catch具体的异常类和直接异常的性能 catch(Excepiton e)和 catch(DetailExcepiton e)比较 * */ private static void exectionCapabilityTest() { long time = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { catchExceptionTest(); } long endTime = System.currentTimeMillis(); long time2 = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { catchDetailException(); } long endTime2 = System.currentTimeMillis(); log.info("catchExceptionTest花费时间:" + (endTime - time)); log.info("catchDetailException花费时间:" + (endTime2 - time2)); } private static void catchDetailException() { try { File file = new File("aa"); FileInputStream fis = new FileInputStream(file); fis.close(); } catch (FileNotFoundException e) { log.error("捕获FileNotFoundException:" + e.getLocalizedMessage()); } catch (IOException e) { log.error("捕获IOException:" + e.getLocalizedMessage()); } } private static void catchExceptionTest() { try { File file = new File("aa"); FileInputStream fis = new FileInputStream(file); fis.close(); } catch (Exception e) { log.error("捕获Exception:" + e.getLocalizedMessage()); } } }
2015-1-16 9:48:13 com.team.gaoguangjin.javabase.exception.ExceptionCapability exectionCapabilityTest
信息: catchExceptionTest花费时间:355
2015-1-16 9:48:13 com.team.gaoguangjin.javabase.exception.ExceptionCapability exectionCapabilityTest
信息: catchDetailException花费时间:15
2015-1-16 10:17:03 com.team.gaoguangjin.javabase.exception.ExceptionCapability exceptionByListTest
信息: catchExceptionTest花费时间:30
2015-1-16 10:17:03 com.team.gaoguangjin.javabase.exception.ExceptionCapability exceptionByListTest
信息: catchExceptionNormal花费时间:1
相关文章推荐
- javaWeb(struts+freemarker)项目,捕获404、异常并做优化处理
- 建设局项目总结(三)——ASP.NET 实现自动捕获异常和异常处理
- 项目SERVICE层捕获异常
- 建设局项目总结(三)——ASP.NET 实现自动捕获异常和异常处理
- try catch 小结 , node的回调callback里不能捕获异常 , 不能被v8优化(现在能了),
- Android自定义捕获Application全局异常优化版
- SpringBootWEB项目和非Web项目的全局异常捕获
- 项目中使用SPRING AOP方式统一捕获异常
- Android中全局异常捕获以及动态logcat打印。方便上线项目分析
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- Android项目中异常捕获及对应Log日志文件保存
- VS2010 F5调试时出现:“ 尝试运行项目时出错:未捕获通过反射调用的方法引发的异常”解决
- 建设局项目总结(三)——ASP.NET 实现自动捕获异常和异常处理
- VS2010 F5调试时出现:“ 尝试运行项目时出错:未捕获通过反射调用的方法引发的异常”解决
- Android编程实现项目中异常捕获及对应Log日志文件保存功能
- J2EE应用性能问题的常用优化项目
- 技巧和诀窍;在VS 2005里优化ASP.NET 2.0Web项目的Build性能
- 全局异常捕获处理
- 抛出异常和捕获异常
- 异常捕获的传参方式