项目中捕获异常的优化

</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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值