0

i have a statement like this that gives an error "missing return statement".But i assigned return statements as you see.whats wrong?

public int compareTo (Object o)
{
    try 
      {
          Sports m = (Sports)o;
          if(this.date.before(m.date)&& o instanceof x)
          {
             return -1;
          }
          else if(this.date.equals(m.date)&& o instanceof x)
          {
            return 0;
          }
          else
          { 
            return 1;
          }
    }

    catch(IllegalArgumentException e) 

    {
        System.out.print("Not an instance of x class");
    }
}
2
  • 1
    Try using proper braces or at least indentation, this code is also not much readable. Commented May 20, 2011 at 13:37
  • multiple returns is bad form and a single return after the catch block is sufficient Commented May 20, 2011 at 14:19

4 Answers 4

5

Yes - if IllegalArgumentException is caught, you're not returning anything.

To be honest, it's very rarely a good idea to catch IllegalArgumentException. Why are you catching it here?

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

4 Comments

i want to catch that exception if o is not an instance of class x.isnt it right to throw an illegal argument exception if x is not an instance of x class.
@cane-r OK, there's several confusions here. instanceof doesn't throw an exception, it just yields true or false. Casting does throw an exception - but ClassCastException, not IllegalArgumentException. Some methods may choose to throw IllegalArgumentException for a wrong argument type. Finally, what is x? A class placeholder?
@robin i might be wrong.how i do this:if object o is not instance of the x class it throws an IllegalArgumentException.its like you enter a double integer in a program but you have to enter an int.so the program throws an exception that says enter an int.similar to this,you have to enter an object that an instance of class x.
@cane-r: If you want to throw an exception, you need to throw it - not catch it.
2

if u want to use catch

int returnResult = -99;
try{

        returnResult = -1 ;
         else 
        returnResult = 0;
         else 
        returnResult = 1;
 }   catch(IllegalArgumentException e) {
    System.out.print("Not an instance of x class");
}
return returnResult;

Comments

0

It's raising a compiler issue because there is a scenario with no return: when an exception is thrown. You need to add a return statement after the catch if you're really going to catch IllegalArgumentException.

1 Comment

After the catch block, or at the end of the catch block.
0
this.date.before(m.date)

it's the only piece of code which could generate IllegalArgumentException. Are you sure that you want to catch it? If you just want to be sure o is instance of x do smth like:

public int compareTo (Object o){
if(o instanceof x) {
    Sports m = (Sports)o;
    if(this.date.before(m.date)&& o instanceof x)
        return -1;
    else if(this.date.equals(m.date)&& o instanceof x)
        return 0;
    else 
        return 1;
}
else {
    System.out.print("Not an instance of x class");
    return 2; 
}

}

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.