简体   繁体   English

从void返回字符串

[英]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(); 你好,我有这个程序设计任务,在这里我必须使用他们给我们使用的函数,因为它们给我们使用,我遇到的问题是这必须是无效的,而且我不允许使用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();? 还是我的问题是如何在不更改方法标头的情况下返回异常,还是使用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 从方法抛出异常,您可以为此使用关键字throw 。尝试上述方法从方法抛出异常

In your catch block do this: 在您的catch块中执行以下操作:

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. 由于IndexOutOfBoundsException是RuntimeException,因此不需要在方法中添加throw声明。

Where ever you call the function you can add a catch block to read the error message like this: 无论在哪里调用该函数,都可以添加catch块来读取错误消息,如下所示:

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. 您可以完全删除try-catch块。 IndexOutOfBoundsException is a runtime exception, so it does not require the throws IndexOutOfBoundsException syntax. IndexOutOfBoundsException是运行时异常,因此它不需要throws IndexOutOfBoundsException语法。

However, if you want the exception to be less cryptic, you can wrap it with your own RuntimeException : 但是,如果您希望该异常不那么隐秘,则可以使用自己的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 删除try..catch块并将您的功能修改为

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!"); 在尝试调用此方法的地方添加try catch并使用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. 即使您没有向该方法添加抛出,而是将deleteItem的调用放在try catch块中,也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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