简体   繁体   中英

Return a string from void

hello I have this programming assignment where I have to use the functions they gave us, as they give it to us to use, the problem i am encountering is the fact this has to be void and I am not allowed to use System.out.println(); either so my question is how to i return the exception without changing the method header or it using System.out.println();?

public void deleteItem(String itemID){
    try {
        index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);
    }
    catch (IndexOutOfBoundsException e) {
        System.out.println("ITEM " + itemID + " DOES NOT EXIST!");
    }
}

You can change your method signature and throw an exception

public void deleteItem(String itemID) throws Exception{
    try {
        index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);
    }catch (IndexOutOfBoundsException e) {
        Exception ex = new Exception("ITEM " + itemID + " DOES NOT EXIST!");
        throw ex;
    }
}

Once done you can get your error message like this

try{
    xxx.deleteItem("your itemID");
}catch(Exception e){
    // You will read your "ITEM " + itemID + " DOES NOT EXIST!" here
    String yourErrorMessage = e.getMessage();
}
public void deleteItem(String itemID){
    try {
        index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);
    }
    catch (IndexOutOfBoundsException e) {
        throw new IndexOutOfBoundsException( "ITEM " + itemID + " DOES NOT EXIST!");
    }
}


    public void deleteItem(String itemID)throws IndexOutOfBoundsException{

        index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);

   } 

You cant return exceptions . Exceptions are thrown from the methods , you can use keyword throw for that.try above methods to throw exceptions from methods

In your catch block do this:

catch (IndexOutOfBoundsException e) {
       throw new IndexOutOfBoundsException("ITEM " + itemID + " DOES NOT EXIST!");
}

You don't need to add a throw declaration to your method, since IndexOutOfBoundsException is a RuntimeException.

Where ever you call the function you can add a catch block to read the error message like this:

catch (IndexOutOfBoundsException ex) {
      System.out.println(ex.getMessage());
}

Well, if the method is used incorrectly (without validation of the index), maybe it should throw an exception?

You can remove the try-catch block completely. IndexOutOfBoundsException is a runtime exception, so it does not require the throws IndexOutOfBoundsException syntax.

However, if you want the exception to be less cryptic, you can wrap it with your own RuntimeException :

public void deleteItem(String itemID){
    try {
        index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);
    }
    catch (IndexOutOfBoundsException e) {
        throw new RuntimeException("Invalid item ID: " + itemID, e);
    }
}

Remove try..catch block and modify your function as

public void deleteItem(String itemID) throws IndexOutOfBoundsException{
         index = Change.indexOf(itemID);
        StockItems.remove(index);
        Change.remove(index);
}

Add try catch where you call this method and use System.out.println("ITEM " + itemID + " DOES NOT EXIST!"); there.

Ya even if you do not add throws to this method but put call of deleteItem in try catch block will work.

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