I'm trying to find the right way to set up the exception handling code for addressing performance hits.. here are some number i got. Basically I want to set it up to to handle a exception call to back end web services.. Any comments on whether one way gives better per formance than the other and why would be appreciated. thank you..
I’ve posted some times for three different coding structures in java
Try time Existing Code Structure throw same exception in catch block:12632
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 1579
Try timeestimatedTimeTryReturnNullAfter Catch Block: 1579
Try time Existing Code Structure throw same exception in catch block:16580
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 2368
Try timeestimatedTimeTryReturnNullAfter Catch Block: 4737
Try time Existing Code Structure throw same exception in catch block:29211
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 3947
Try timeestimatedTimeTryReturnNullAfter Catch Block: 2369
Try time Existing Code Structure throw same exception in catch block:9869
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 1974
Try timeestimatedTimeTryReturnNullAfter Catch Block: 1579
Try time Existing Code Structure throw same exception in catch block:13422
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 1579
Try timeestimatedTimeTryReturnNullAfter Catch Block: 1579
Try time Existing Code Structure throw same exception in catch block:9474
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 1579
Try timeestimatedTimeTryReturnNullAfter Catch Block: 1973
Try time Existing Code Structure throw same exception in catch block:5922
Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: 789
Try timeestimatedTimeTryReturnNullAfter Catch Block: 789
public class Overhead {
public static void main(String[] args) {
String testString = "";
long startTimeTry = System.nanoTime();
tryTrimThrowSameException(testString);
long estimatedTimeTry = System.nanoTime() - startTimeTry;
long startTimeIf = System.nanoTime();
ifTrim(testString);
long estimatedTimeIf = System.nanoTime() - startTimeIf;
long startTimeTryThrowsinCatchBlock = System.nanoTime();
tryTrimThrowsChildExceptioninCatchBlock(testString);
long estimatedTimeTryThrowsinCatchBlock = System.nanoTime() - startTimeTryThrowsinCatchBlock;
long startTimeTryReturnNull = System.nanoTime();
tryTrimReturnNull(testString);
long estimatedTimeTryReturnNull = System.nanoTime() - startTimeTryReturnNull;
System.out.println("Try time Existing Code Structure throw same exception in catch block:" + estimatedTimeTry);
//System.out.println("If time:" + estimatedTimeIf);
System.out.println("Try timeestimatedTimeTryThrowsChild Exception inCatchBlock: " + estimatedTimeTryThrowsinCatchBlock);
System.out.println("Try timeestimatedTimeTryReturnNullAfter Catch Block: " + estimatedTimeTryReturnNull);
}
public static String tryTrimThrowSameException(String raw) {
try {
return raw.trim();
}
catch (java.lang.Exception e ) {
//throw new java.lang.NullPointerException() ;
System.out.println("throws an exception") ;
throw e ;
}
//return null;
}
public static String tryTrimThrowsChildExceptioninCatchBlock(String raw) {
try {
return raw.trim();
}
catch (java.lang.Exception e ) {
System.out.println("throws an exception") ;
throw new java.lang.NullPointerException() ;
//throw e ;
}
// return null;
}
public static String tryTrimReturnNull(String raw) {
try {
return raw.trim();
}
catch (java.lang.Exception e ) {
//throw new java.lang.NullPointerException() ;
System.out.println("throws an exception") ;
//throw e ;
}
return null;
}
public static String ifTrim(String raw) {
if (raw == null) {
return null;
}
return raw.trim();
}
}