Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling

 
Ali Ekber
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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;
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • 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.
 
Ramesh Shanmugam
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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) ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic