This topic illustrates the performance improvement techniques in Exceptions with the following sections:
Java provides an efficient way to handle unexpected conditions that can occur in the program. When there is an error or bug in the program then the program terminates as soon as an error is encountered leaving the program in an in consistent state, to avoid this Java makes use of Exception Handling mechanism to a great advantage so that when ever there is an exceptional condition then the program handles it gracefully and continues program execution.
The whole concept of exceptions/errors is handled by the java.lang.Throwable class. It has two subclasses - Error and Exception. We generally need not handle Errors, they are handled by JVM. Example of Error is OutOfMemoryError.
Exceptions are of two types -Checked exceptions and Unchecked exceptions.
Checked exceptions should either be declared in the throws clause or caught in the catch block. Unchecked exceptions need not be declared in the throws clause but can to be caught in the catch clause.
Note: This section assumes that reader has some basic knowledge of Java Exceptions.
Optimization techniques in Exceptions
In a catch block avoid using the generic class Exception. For each try block use specific catch blocks based on what can go wrong in your code.
Do not use Exception handling for anything other than exception handling like to control the flow of your program.
Whenever you are using a throws clause always use the specific subclass of Exception like FileNotFoundException rather than using throws Exception.
Use exception handling generously-Very little overhead is imposed by using exception handling mechanism unless an exception occurs. But when an exception occurs it imposes an overhead in terms of execution time.
Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This prevents resource leaks even if an exception occurs.
When using method calls always handle the exceptions in the method where they occur, do not allow them to propagate to the calling method unless it is specifically required. It is efficient to handle them locally since allowing them to propagate to the calling method takes more execution time.
Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark.
package com.performance.exception;
public class ExceptionTest{
public static void main(String args[]){
long start,end;
int i =0;
int[] intArray = new int[25000];
String stringArray[] = new String[25000];
int size = stringArray.length;
for(i=0;i<size;i++){
if(i%50 == 0)
stringArray[i]="hello world";
else
stringArray[i]= Integer.toString(i);
}
start=System.currentTimeMillis();
for(i=0;i<size;i++){
try{
intArray[i]=Integer.parseInt(stringArray[i]);
} catch(NumberFormatException e) {
}
}
end=System.currentTimeMillis();
System.out.println(end-start + " millis with try/catch inside for loop ");
start=System.currentTimeMillis();
try{
for(i=0;i<size;i++){
intArray[i]=Integer.parseInt(stringArray[i]);
}
} catch(NumberFormatException e) {
}
end=System.currentTimeMillis();
System.out.println(end-start + " millis with try/catch outside for loop ");
}
}
The output is
50 millis with try/catch inside for loop
0 millis with try/catch outside for loop
Key Points
- Overview of Exceptions
- Optimization techniques in Exceptions
- Key Points
Java provides an efficient way to handle unexpected conditions that can occur in the program. When there is an error or bug in the program then the program terminates as soon as an error is encountered leaving the program in an in consistent state, to avoid this Java makes use of Exception Handling mechanism to a great advantage so that when ever there is an exceptional condition then the program handles it gracefully and continues program execution.
The whole concept of exceptions/errors is handled by the java.lang.Throwable class. It has two subclasses - Error and Exception. We generally need not handle Errors, they are handled by JVM. Example of Error is OutOfMemoryError.
Exceptions are of two types -Checked exceptions and Unchecked exceptions.
Checked exceptions should either be declared in the throws clause or caught in the catch block. Unchecked exceptions need not be declared in the throws clause but can to be caught in the catch clause.
Note: This section assumes that reader has some basic knowledge of Java Exceptions.
Optimization techniques in Exceptions
In a catch block avoid using the generic class Exception. For each try block use specific catch blocks based on what can go wrong in your code.
Do not use Exception handling for anything other than exception handling like to control the flow of your program.
Whenever you are using a throws clause always use the specific subclass of Exception like FileNotFoundException rather than using throws Exception.
Use exception handling generously-Very little overhead is imposed by using exception handling mechanism unless an exception occurs. But when an exception occurs it imposes an overhead in terms of execution time.
Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This prevents resource leaks even if an exception occurs.
When using method calls always handle the exceptions in the method where they occur, do not allow them to propagate to the calling method unless it is specifically required. It is efficient to handle them locally since allowing them to propagate to the calling method takes more execution time.
Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark.
package com.performance.exception;
public class ExceptionTest{
public static void main(String args[]){
long start,end;
int i =0;
int[] intArray = new int[25000];
String stringArray[] = new String[25000];
int size = stringArray.length;
for(i=0;i<size;i++){
if(i%50 == 0)
stringArray[i]="hello world";
else
stringArray[i]= Integer.toString(i);
}
start=System.currentTimeMillis();
for(i=0;i<size;i++){
try{
intArray[i]=Integer.parseInt(stringArray[i]);
} catch(NumberFormatException e) {
}
}
end=System.currentTimeMillis();
System.out.println(end-start + " millis with try/catch inside for loop ");
start=System.currentTimeMillis();
try{
for(i=0;i<size;i++){
intArray[i]=Integer.parseInt(stringArray[i]);
}
} catch(NumberFormatException e) {
}
end=System.currentTimeMillis();
System.out.println(end-start + " millis with try/catch outside for loop ");
}
}
The output is
50 millis with try/catch inside for loop
0 millis with try/catch outside for loop
Key Points
- Be specific while handling the exception in your catch block.
- Be specific while throwing exception in your throws clause.
- Do not use Exception Handling to control programming flow.
- Very little overhead is imposed by using exception handling mechanism unless an exception occurs or thrown a new exception object explicitly.
- Always use the finally block to release the resources to prevent resource leaks.
- Handle exceptions locally wherever possible.
- Do not use Exception handling in loops.
No comments:
Post a Comment