一段测试try...catch运行时间的代码

本文探讨了Java中异常处理机制的应用,并通过具体代码示例展示了try-catch-finally块的功能。此外,还对比了使用try-catch与不使用时的性能差异,为开发者提供了关于如何高效地进行异常处理的实践指导。

public class Test2 {

    private static int test() {
        int i=1;

        try {
            i=2;
            return i;
        } catch (Exception e) {
            i = 3;
            return i;
        } finally {
            i = 4;
            System.out.println(i);
        }
       
    }

    public static void main(String[] args){
     double b = 0;
     String s = "";
     long ii = System.currentTimeMillis();
        for(int i = 0; i<20000;i++) {
         try {
          b = StrictMath.pow(b+i, 1.0/3);
          s = s + b+";";
         }catch(Exception e){
          e.printStackTrace();
         }finally {

         }
        }
       
        //System.out.println("b = "+b + "\ns = "+ s);
        System.out.println(System.currentTimeMillis()-ii);
       
     double c = 0;
     String ss = "";
     long iii = System.currentTimeMillis();
        for(int i = 0; i<20000;i++) {
          c = StrictMath.pow(c+i, 1.0/3);
          ss = ss + c+";";
        }
       
        //System.out.println("b = "+c + "\nss = "+ ss);
        System.out.println(System.currentTimeMillis()-iii);
       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值