简体   繁体   English

Java异常处理不返回任何值

[英]Java exception handling returning no value

I'm trying to understand how I can use java exception handling to return just the exception message when the input is invalid. 我试图了解如何在输入无效时使用Java异常处理仅返回异常消息。 As far as I understand I have to use return out of try catch, to get this compiled (or in both). 据我了解,我必须使用try catch之外的return来进行编译(或同时进行编译)。 But actually I don't want to return anything if the input parameter is invalid. 但是实际上,如果输入参数无效,我不想返回任何内容。

If I was dealing with strings, there would be null. 如果我正在处理字符串,则将为null。 But that doesn't seem to work with int. 但这似乎不适用于int。

Is there no way doing this? 没办法吗?

public class Arrays {

public static int[] intArray = {1,2,3,4,5,60,7,8,9,10};


public static int arrayGet(int[] array, int i){
    try{
        return intArray[i];

    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("Please enter number between 0 and "+i);
    }
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println(arrayGet(intArray,11));
}
}

The code my not make a lot of sense, but I'd like to understand how to deal with the general situation. 该代码没有多大意义,但我想了解如何处理一般情况。

You will have to return some value (which must be an int). 您将必须返回一些值(必须为int)。 With Strings you don't have such a problem because String is not a Standard data type and therefore can be Null.But here you will have to return some value in the catch statement. 使用Strings不会遇到这样的问题,因为String不是Standard数据类型,因此可以为Null,但是在这里您必须在catch语句中返回一些值。

At present your arrayGet is not compilable. 目前,您的arrayGet无法编译。 If you want to return a String in case of error you can do this in catch block of arrayGet like 如果要在发生错误的情况下返回String,可以在arrayGet的catch块中执行此操作

} catch (ArrayIndexOutOfBounds e) {
    throw new Exception("my message");
}

And in the main method 而在主要方法

try {
    int i = arrayGet(11);
} catch (Exception e) {
    String msg = e.getMessage();
}

The common super type of int and String is Object (after boxing). intString的常见超级类型是Object (装箱后)。 That means you can return a String or int if you declare the return type as Object : 这意味着如果您将返回类型声明为Object则可以返回Stringint

public static Object arrayGet(int[] array, int i){
    try{
        return intArray[i];    
    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("Please enter number between 0 and "+i);
        return e.getMessage();
    }
}

But the caller will not know which type was returned, so they can only really use it as an Object . 但是调用者将不知道返回了哪种类型,因此他们只能将其真正用作Object

Object o = arrayGet(array, 11);
// o is maybe an int, or maybe a String. But it's definitely an Object.

In this case the arguments of the method are the culprit. 在这种情况下,方法的参数是罪魁祸首。 A way to let the caller know is by throwing an IllegalArgumentException : 让调用者知道的一种方法是抛出IllegalArgumentException

public static int arrayGet(int[] array, int i){
    if(i < 0 || i >= array.length)
        throw new IllegalArgumentException("Please enter number between 0 and " + i);

    return intArray[i];
}

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

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