Results 1 to 2 of 2

Thread: The return type

  1. #1
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default The return type

    Is there a way for a method to not return anything if a certain condition is met, despite the fact that I have specified an integer return type for that particular method?

    Thanks
    Marcus:cool:

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    Only way I can think of is to have the method throw an exception, like this:

    Java Code:
    public int someMethod(int input) throws SomeException
    {
    
    if(input <= 0)
    {
    throw new SomeException("Input must be greater than zero.");
    }
    else
    {
    //do something
    return something;
    }
    
    }
    Greetings
    Felissa:p

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 09:05 PM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 12:25 PM
  3. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 05:20 PM
  4. return type determines override/overload?
    By hedefalk in forum Advanced Java
    Replies: 4
    Last Post: 07-11-2007, 02:48 PM
  5. Return to try block
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-11-2007, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •