• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have below code in a JSP as a method. When the date is null, the method throws StringIndexOutOfBoundsException and stops executing. Isn't this the whole point of putting the try/catch block in this code? Why does the code stop executing (stop loading the page)?

-------------------------------------------------
private String parseDate(String date) {
String tmp = "";
try {
tmp = date.substring(4,6) + "/" + date.substring(6,8) + "/" + date.substring(0,4);
} catch (Exception x) {
tmp = "";
}
return tmp;
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When "date" is null, you'd see a NullPointerException, not an index exception; you'd see the latter if "date" were the empty string "".

Regardless of which was thrown, this method should complete. What makes you think that it's this method that's raising the error? Perhaps it's another method that tries to use the empty String that this one returns.
 
Ali Ekber
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the error in Tomcat log file, also when I comment out the substring statements, I don't get the exception (so I am pretty sure that this is the statement causing the exception and page stop loading).
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Ekber:
I see the error in Tomcat log file, also when I comment out the substring statements, I don't get the exception (so I am pretty sure that this is the statement causing the exception and page stop loading).




Then the date cannot possibly be null; it's a range problem. Couple side notes: you should rarely if ever catch Exception and it's not good form to place code on a JSP nowadays.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better you can check at the first itself whether the date is null like giving date.equals("null") if so dont do anything.. or display what u would like to do.. in the else case .. you parse the string to your own format..
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Shanmugam:
date.equals("null")



Aaaaaahk!

First, if date is null, this will give a NullPointerException, because you're calling a method on it, so this can never succeed! Second, the String "null" is not at all the same as the literal constant null!

To check if the variable date is null, you'd write

if (date == null) ...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic