Intuitively I feel that the try catch has a performance impact on the program, but the following experiments can show that in some cases try-catch is only a logical effect, with no significant drag on performance
Public Static voidMain (string[] args)throwsException {intn = 10000; for(intj = 0; J < 20; J + +) {System.out.println ("Case" + (j + 1)); LongStart =System.currenttimemillis (); for(inti = 0; I < n; i++) { Try{testprocess (); } Catch(Exception e) {//Todo:handle Exception}} System.out.println (System.currenttimemillis ()-start); Start=System.currenttimemillis (); for(inti = 0; I < n; i++) {testprocess (); } System.out.println (System.currenttimemillis ()-start); } } Public Static voidtestprocess () {intc = 1; for(inti = 1; I < 10000; i++) {C*=i; if(c% 2 = = 0) {C+=i; } if(i = = 0) { Throw Newruntimeexception (); } } }
Results:
case1 305297case2 285307CASE3 312312case4 306310case5 294296case6 286302case8 276283case9 287291case10 256271case11 256283case13 263287case14 262281case15 254279case16 254281case17 250275case18 262295case20 254288
The impact of Java Try-catch on performance