简体   繁体   English

抛出自定义异常(如果是for循环)

[英]Throwing custom Exceptions, if for loop

public class StringArray {
    private String strArr[];

    public StringArray(int capacity) {
       strArr = new String [capacity];
    }

    public int indexOf(String s) throws StringNotFoundException {
        for(int i=0;i<strArr.length ;++i) {
            if (strArr[i].equals(s)) {
                return i;
            } else {
                throw new StringNotFoundException();
            }
        }
    }
}

What I want to do is to return the index of the string I'm looking for if it's in the array, to throw an exception otherwise. 我想做的是返回我要查找的字符串的索引(如果它在数组中),否则抛出异常。

However Eclipse says I've got to return an int. 但是Eclipse表示我必须返回一个int。

So should I just change the return type to void or are there other options? 所以我应该只将返回类型更改为void还是有其他选择?

StringNotFoundException is a custom exception I've made up. StringNotFoundException是我创建的一个自定义异常。

do like this 像这样

public int indexOf(String s) throws StringNotFoundException {
     for(int i=0;i<strArr.length ;++i) {
         if (strArr[i].equals(s)){
             return i;
         }
     }
     throw new StringNotFoundException();
}

Why return -1 here? 为什么在这里返回-1? here is the code: 这是代码:

public int indexOf(String s) throws StringNotFoundException {
    for(int i=0; i<strArr.length ;++i) {
        if (strArr[i].equals(s)) {
            return i;
        }
    }
    throw new StringNotFoundException();
}

The fact that you're not finding the String you're looking for is not enough to justify the usage of Exception . 找不到要查找的String的事实不足以证明Exception的用法是正确的。 It's not an exceptional case, you know it's going to happen, you're saying that in your code. 这不是一个例外的情况,您知道它会发生,您在代码中这么说。

Your code should reflect this. 您的代码应反映出这一点。 You should not return custom values, that is adding a meaning to things like -1 or the likes, which is not correct. 您不应该返回自定义值,即为诸如-1之类的东西添加含义,这是不正确的。

More on the subject: Should a retrieval method return 'null' or throw an exception when it can't produce the return value? 有关此主题的更多信息: 检索方法是否应返回'null'或在无法产生返回值时引发异常?

You need to iterate through each string in the array and only if nothing matches, throw the exception. 您需要遍历数组中的每个字符串,并且仅当没有匹配项时才抛出异常。

I think this is what you want : 我想这就是你想要的:

public int indexOf(String s) throws StringNotFoundException {
        for (int i = 0; i < strArr.length; ++i) {
            if (strArr[i].equals(s)) {
                return i;
            } 

        }
        throw new StringNotFoundException();

    }

How about: 怎么样:

/** Returns index of String s in array strArr.  Returns -1 if String s is not found. */
public int indexOf(String s) {
        for(int i=0;i<strArr.length ;++i) {
            if (strArr[i].equals(s)){
             return i;
            }
        return -1;
}

Avoids using an exception altogether. 避免完全使用异常。

Try this.. 尝试这个..

public int indexOf(String s) throws StringNotFoundException {

       int index = Arrays.binarySearch(strArr ,s);

        if( index > 0)
             return index;
        else
            throw new StringNotFoundException();
    }

Why you want to do this ? 为什么要这样做? .

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

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