2

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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot... I will change my way of handling exceptions...:)
0

You have indicated the return command in side the try statement try this way it will work.

public int x(){

    try{

    }catch (Exception e){}

    return x;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.