简体   繁体   中英

How do you return the result of a method in java?

This is a follow-on from my question regarding multiple catch blocks How to deal with returning an object and handling errors

My updated code is below, however I'm getting an error that my draw() method must return an int, is there any way to make the compiler recognise that endOfDeck() will return an int, thus fulfilling the return requirements? (see "return endOfDeck())

Any smarter way to accomplish this?

import java.util.ArrayList;
import java.util.ListIterator;
/**
 * @author L
 *
*/
public abstract class Deck 
{

private ArrayList<Card> cards;
private ListIterator<Card> deckPosition = cards.listIterator();
private Card lastDrawn;
/**
 * 
 */
public Deck() 
{   
}

public int draw()
{

    try
    {
        if(deckPosition.hasNext())
        {
            lastDrawn = deckPosition.next();
            return 1;
        }
        else if(cards.isEmpty())
        {
            throw new EmptyDeckException();
        }
        else
        {
            throw new EndOfDeckException();
        }
    }

    catch(EmptyDeckException e)
    {
        emptyDeck();
        return 0;
    }
    catch(EndOfDeckException e)
    {
        return endOfDeck();

    }
    catch(Exception e)
    {
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Card");
        e.printStackTrace();
    }

}

public abstract int endOfDeck();
public abstract void emptyDeck();

}

Your last catch block does not have any return statement: -

catch(Exception e)
    {
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Card");
        e.printStackTrace();
    }

So, your method will not return any value, if that catch block is executed. May be you can return any integer denoting the exception, or wrap the exception in a RuntimeException and rethrow it.


But, your code seems somewhat problematic. You are throwing exception unnecessarily. Whatever you are doing in the catch block, you can do directly in your if-else blocks.

So, you can modify your code to: -

    try {
        if(deckPosition.hasNext())
        {
            lastDrawn = deckPosition.next();
            return 1;
        }
        else if(cards.isEmpty())
        {
            emptyDeck();
            return 0;
        }
        else
        {
            // throw new EndOfDeckException();  // Don't throw it
            return endOfDeck();
        }

    } catch(Exception e) {
        /** Also, if possible, replace this catch block with the one catching
            more specific exception than all the `Exceptions` **/
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Card");
        throw new RuntimeException("Exception drawing a card");
    }

Your bottom catch must return an int to satisfy all code paths.

catch(Exception e)
    {
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Card");
        e.printStackTrace();
        // Return needs to be here.
    }

or, you need to add a default return value at the bottom of the method.

When a method is declared as returning something you have to make sure that every path of execution through the method results in an (uncaught) exception getting propagated to the caller or a value being returned.

Your statement return endOfDeck() is perfectly fine, but you have a code path that still doesn't return:

catch(Exception e)
    {
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Card");
        e.printStackTrace();
    }

If this case occurs then there's noting the compiler can return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM