您的位置:首页 > 其它

项目中捕获异常的优化

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐