cutie_smily
asked on
how to check if ia m having null values..
I am getting recvords from database values in XML Object ..so i retriving using XPath as shown below
String userPhone=new String( this.xpathFactory.newXPath ().evaluat e( "/row/UADD_PHNE", document ) );
How do i check above value is null then i need to throw an exception (Runtime exeception where if null needs to skip that record) i do not want the null string into my flat file
What is try{ } catch( Throwable e ) { //How is Throwable different from Exception e
or NullpointerException e
String userPhone=new String( this.xpathFactory.newXPath
How do i check above value is null then i need to throw an exception (Runtime exeception where if null needs to skip that record) i do not want the null string into my flat file
What is try{ } catch( Throwable e ) { //How is Throwable different from Exception e
or NullpointerException e
You could only check if the userPhone value is empty.
you should catch an Exception instead of Throwable. because when you catch throwable it is possible that you handle all unexpected errors.
go to this url for a short description: http://wiki.answers.com/Q/What_is_the_difference_between_catch_exception_e_catch_error_err_and_catch_throwable_t
Try this url for further information on exceptions: http://java.sun.com/docs/books/tutorial/essential/exceptions/
you should catch an Exception instead of Throwable. because when you catch throwable it is possible that you handle all unexpected errors.
go to this url for a short description: http://wiki.answers.com/Q/What_is_the_difference_between_catch_exception_e_catch_error_err_and_catch_throwable_t
Try this url for further information on exceptions: http://java.sun.com/docs/books/tutorial/essential/exceptions/
if (!userPhone.isEmpty()) {
throw new NullPointerException("empty value in userPhone");
}
Throwable is the superclass of Exception and Error.
Error is usually not handled.Exceptions are of two types.Unchecked and checked exceptions.
Unchecked exceptions are not usually handled unlike the checked exceptions.
So for a good design you code as below.
if(!userPhone.isEmpty())
{note the not symbol, and perform the required operation}
but if you require to handle it then
throw new NullPointer Exception() in the condition.
We usually dont catch or throw general exceptions like RunTimeException, the exceptions need to be more specific.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry! thought of splitting point between answers. I could not get how to do that..is there a way to get back do
How the check for null value:
String userPhone = this.xpathFactory.newXPath
if (userPhone == null) {
How to throw an exception:
throw new RuntimeException();
What is try-catch:
You can use this construct to catch an exception. This means you can define what should happen, if an exception of type X occurs. This can be useful to recover from errors without ending your process. For more detail on try-catch please consult a java handbook or try google.
Throwable:
The Throwable class is the superclass of all errors and exceptions in the Java language. Instances of two subclasses, java.lang.Error and java.lang.Exception, are conventionally used to indicate that exceptional situations have occurred.
Hope this helps.