This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Ali Ekber
Ranch Hand

Joined: Jun 12, 2005
Posts: 41
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ali Ekber
Ranch Hand

Joined: Jun 12, 2005
Posts: 41
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

Joined: Sep 19, 2004
Posts: 531
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

Joined: Sep 13, 2004
Posts: 132
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..


Ramesh Shanmugam - SCJP 1.5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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) ...
 
Consider Paul's rocket mass heater.
 
subject: Exception handling
 
Similar Threads
Best way to convert date from MM/dd/yyyy to yyyy-mm-dd ?
SimpleDateFormat and Timezones
XML Schema Validation with Xerces
Uploading excel sheet to Database
Calendar Printing problem