Showing posts with label Exceptions. Show all posts
Showing posts with label Exceptions. Show all posts

Wednesday, April 6, 2011

try with resource block

Often we use the try block while using resources like Streams and Connections. In these cases it is mandatory to close the resources after the completion of the task. We usually do the same in the finally block as in the example below

BufferedReader br = new BufferedReader(new FileReader(path));

try {

return br.readLine();

} finally {

br.close();

}

In Java 7 this process has been shortened by the try-with-resource statement. In this the resources are implicitly closed on the completion of the try fragment irrespective of whether the code was successfully executed or with exception. The only change is that the resource must implement the java.lang.AutoCloseable interface. The classes java.io.InputStream, OutputStream, Reader, Writer, java.sql.Connection, Statement, and ResultSet have been retrofitted to implement the AutoCloseable interface and can all be used as resources in a try-with-resources statement.

The Catch Blocks

Java 7.0 has come up with a new feature where it is possible to catch multiple exceptions in a particular catch block. For example

catch (IOException | SQLException ex) {

logger.log(ex);

throw ex;

}

In this case both IOException and SQLException will be caught in the same block.It has to be noted though that the parameter is implicitly final i.e the parameter cannot be assigned to any other Object.

Thursday, January 6, 2011

java.lang.NoSuchFieldError: IS_JAVA_1_6

Issue/Exception :

While working on Spring MVC validation faced this exception when using java 1.6 with Spring 2.5.5/Hibernate Validator 4.0.2.GA .

I guess this exception is not related to Spring / Hibernate Validator but common to any api which when run in Java 1.6 environment

Solution :

This issue was solved by putting commons-lang-2.5.jar in the classpath instead of older version of commons-lang library .

Also if the used server has an older version of commons-lang that should be removed

Document root element "beans", must match DOCTYPE root "null".

Issue/Exception :

A common error while working with Spring is the the Exception,

Document
root element "beans", must match DOCTYPE root "null".

Though everything on the spring-config.xml file looks right we get the error. The exception stack trace is

Caused by: org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath*:cfa-spring-context.xml], factory key [cfaBeanFactory]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Line 16 in XML document from URL [file:/C:/.../cfa-spring-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:389)
... 56 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Line 16 in XML document from URL [file:/C:/.../cfa-spring-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:169)
... 59 more
Caused by: org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
Solution :

This happens usually when we use an older version of Spring in the classspath. I replaced the older spring version of 2.5.0 with the latest version of 2.5.5 which solved the issue.


Solution Source

http://stackoverflow.com/questions/266601/spring-configuration-error

Tuesday, November 2, 2010

Difference between NoClassDefFoundError & ClassNotFoundException

java.lang.NoClassDefFoundError is thrown when we try create an object of a class and the runtime environment is not able to load the given class. This happens when we create the object using the new operator. The searched class though existed when the calling class was compiled but at run time the classloader wasn't able to find the binary code.

java.lang.ClassNotFoundException is thrown when a class is being loaded by the class loader. The class is presumed to be present in any the jar files located in the classpath,or lib. This exception is thrown when the forName method in the class or the findSystemClass/loadClass methods in the ClassLoader is being used.

The following program demonstrates the two , the first method createNew throws the NoClassDefFoundError and the second method loadClass throws ClassNotFoundException.


public class TestException {
public static void main(String[] args) {
createNew();
loadClass();
}
private static void createNew(){
FirstClass fc= new FirstClass();
}
private static void loadClass(){
try {
Class cla11 = Class.forName("collectionsTest.FirstClass");
FirstClass firstClass = (FirstClass)cla11.newInstance();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


A very good post on this is provided here