Saturday, December 3, 2011

Fix for Common exceptions in java language

Java Exception Handling

Exception handling is a very important part of Java application development. It helps to make application stable and error free. When application results error, java handles using exception classes. The Exception can be handled using try/catch/finally blocks.
During development, Developers used to encounter different type exceptions frequently. This post is about listing out the list of common exceptions, and solutions for fixing them.

Following are the exceptions in java programming language. 

NullPointerException in java:-

This is a common Runtime exception that is encountered. This will happens when you are calling the object's method with a null object reference, meaning object is not yet created or not initialized properly.


  • Manipulating object state using a null object
  • Calling method using a null object
  • Any operation that you are doing on a null object


String str=null; 
System.out.println(str.toString());
The solution in our code is.

if(str==null){ 
//initialize the string object here
 }else{
 System.out.println(str.toString()); 
}
To avoid NullPointerException in java, please make sure that the object is initialized properly before accessing their methods/state. Add condition logic for handling the null check.

Java ClassCastException error 

This is a runtime exception, the code throws an error when you are casting an object to the class which is not part of the instance.
For example, in the below code, casting Integer to String and String is not a child class of Integer. and it results in java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String
Object intObject = Integer.valueOf(5793);
String str = (String)intObject;  
Fix for ClassCastException is to please make sure that you are converting correct types, not invalid types.

java NumberFormatException error

This Runtime exception thrown when a string that does not contains a parsable number for Integer conversion.object.
In the below code, parseInt() method is called with a string which contains characters, and the result is java.lang.NumberFormatException: For input string: "s123".

try {
 int i = Integer.parseInt("s123");
} catch (NumberFormatException ex) {
      ex.printStackTrace();
}
Fix for NumberFormatException is please use try and catch handlers and handle this case as expected.

java SocketTimeoutException error

This is a runtime exception thrown when there is an HTTP communication over socket layer between different applications. with sockets communication between client and server. server not accepting sockets accept/read operations or unable to respond for a specific time. From a developer point of view, we don't have control over the server side if it is External third-party Server
From the client side, Fix for SocketTimeoutException is configure socket timeout and handle exception using try and catch blocks

java OutOfMemoryException error:-

This is RuntimeException which occurs when an object exists on heap memory is full. The fix is either increase heap memory or fine tune the code to minimize object creation.
You can configure JVM parameters to increase heap size at server level


Related article


EmoticonEmoticon