views:

49

answers:

1

I have a method wherein i have to return a 2D String array.

The part of code for that method is as follow:-

public String[][] retrieveData(){
 try{
  int noOfRows = 0;
  pstmt = con.prepareStatement("select * from category");
  ResultSet rs = pstmt.executeQuery();
  while(rs.next())
   noOfRows++;
  rs.first();
  categoryContent = new String[noOfRows][noOfCols];
  for(int i = 0 ; i < noOfRows ; i++){
   for(int j = 0 ; j < noOfCols ; j++){
    if(j == 0){
     Integer categoryNo = new Integer(rs.getInt(1));
     categoryContent[i][j] = categoryNo.toString();
    }
    else{
     categoryContent[i][j] = rs.getString(j+1);
    }
   }
   rs.next();
  }
  return categoryContent ;
 }
 catch(Exception e){
  e.printStackTrace();
 }
}

The error which i am getting at compile time is as follows:-

I:\DynamicWebpageCreator\WEB-INF\classes>javac *.java
Category.java:134: missing return statement
        public String[][] retrieveData(){
                                        ^**
1 error

Please help me soon. I am stuck with it. All the answers are highly appreciated!

+4  A: 

If an exception is thrown, you'll print the stack trace, but never return anything. That's what the compiler is complaining about.

In general, "handling" exceptions like this is a really bad idea:

  • Log any exception information somewhere more appropriate than just the console
  • Don't catch bare Exception - catch specific exceptions
  • If you can't actually handle an exception, let it propagate up the call stack

In this case I'd suggest you should probably just change your method to declare which exceptions might be thrown, and remove the try/catch block.

If you genuinely want to catch exceptions (specific ones, mind) then you'll need to work out what you want to return in that case.

Jon Skeet
Thanks a lot...I will change my way of handling exceptions...:)
Shahensha