Java Exception solutions
Exception
handling is a basic part of Java application development. It helps to make the application stable and error-free. When the application results error, java handles using exception classes.
The Exception handle uses try
, catch
, and finally
blocks.
During development, Developers used to encounter different types of exceptions frequently. This post is about listing out the list of common exceptions, and solutions for fixing them.
Following are the exceptions in a java programming language.
NullPointerException in java
This is a common Runtime
exception that is encountered. It happens when you are calling the object’s method with a null object reference, meaning the 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 its methods/state. Add condition logic for handling the null check.
ClassCastException error in java
It 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 please make sure that you are converting correct types, not invalid types.
java NumberFormatException error
This Runtime exception is thrown when a string does not contain a parsable number for Integer conversion.
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 please use try and catch handlers and handle this case as expected.
java SocketTimeoutException error
It is a runtime exception thrown when there is an HTTP communication over the 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’s point of view, we don’t have control over the server-side if it is an External third-party Server.
From the client side, Fix for SocketTimeoutException is configuring socket timeout and handling exceptions using try
and catch
blocks.
java OutOfMemoryException error
It is a RuntimeException
that occurs when an object that 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 the server level.