【问题标题】:Modifying IllegalArgumentException修改 IllegalArgumentException
【发布时间】:2014-01-25 14:28:05
【问题描述】:

我有以下几行代码,我应该编写一个 IllegalArgumentException 异常,这样当我运行它时,它会返回“波兰已经存在。”。我已经阅读了很多关于异常的内容,但我只是不知道我应该如何编写 IllegalArgumentException,在它的构造函数中放入什么等(我只知道我必须有一个构造函数和一个 getMessage() 方法消息。)

更具体地说:如何更改 IllegalArgumentException 的“原因”:如何检查国家名称的相似性?

  public static void main(String[] args) {
    Country country1 = new Country("Poland");

    try{
        Country country = new Country("Poland");
    }
    catch(IllegalArgumentException ie){
        System.out.println(ie.getMessage());
    }
}

【问题讨论】:

  • 制造一个 IllegalArgumentException 不会比choosing which constructor(s) to use 更多。您的问题的答案实际上可能取决于您的 Country 课程。例如,波兰已经存在意味着什么?你如何跟踪它(如果你这样做的话)?
  • 我不知道,这就是为什么我不明白我应该如何检查名称的相似性。考虑到以上是我的整个主要方法,它应该在 Country 类或异常类中的某个地方,但我对异常的理解不够好,所以我很困惑?
  • 基本上,您似乎应该修改 Country。 @MarkoTopolnik 写的答案几乎向您展示了除了跟踪 Country 实例之外您需要用于异常的确切逻辑。
  • 但这意味着没有getMessage方法,正如我所说,我的主要方法必须与上面的完全相同,因此我必须为其创建一个新的自定义异常......带我回到广场 1

标签: java exception exception-handling try-catch illegalargumentexception


【解决方案1】:

你不需要定义一个新的异常类,你只需要抛出现有的。

public Country(String name) {
  if (INSTANCES.contains(this)) 
    throw new IllegalArgumentException(name + " already exists.");
}

需要更多代码来维护INSTANCES 集。

【讨论】:

  • 问题是我应该使用上面main方法的确切格式,所以我必须有一个.getMessage方法,因此,必须有一个新的异常类......
  • IllegalArgumentException 已经具有该方法。它返回你传递给构造函数的内容。
【解决方案2】:

简单的事情

class IAE
{
    public static void main(String args[])
    {
        throw new IllegalArgumentException("Poland doesn't exist");
    }
}

会输出

Exception in thread "main" java.lang.IllegalArgumentException: Poland doesn't exist
    at IAE.main(iae.java:6)

您也可以subclass the exception,但根据您的描述,这似乎没有必要。

【讨论】:

  • 我的整个主要方法必须是上面的,我应该使用try-catch块,我看不出你的回答对我有什么帮助......
【解决方案3】:

有一个例外:实际上只是构建一个新的问题。所有异常都已使用getMessage 方法设置。所以你只需用你想要的消息实例化一个。

IllegalArgumentException(String)

重复的 Country 是另一回事。这种依赖于 Country 类本身,但一个非常简单的方法就是使用 Set<String>

class Country {
     private static final HashSet<String> EXISTING = new HashSet<String>();

     private final String name;

     Country(String name) {
         if(EXISTING.contains(name))
             throw new IllegalArgumentException(name + " already exists!");

         this.name = name;
         EXISTING.add(name);
     }
}

没关系,但如果 Country 更复杂,可能就没那么简单了。还有一个问题是,这意味着国家只能构建一次,即使它们被垃圾收集。

如果国家本身可以“重用”,则可以像下面这样微弱地跟踪它们:

class Country {
    private static final HashMap<String, Reference<Country>> EXISTING = (
        new HashMap<String, Reference<Country>>()
    );

    private final String name;

    Country(String name) {
        WeakReference<Country> existing = EXISTING.get(name);
        if(existing != null && existing.get() != null)
            throw new IllegalArgumentException(name + " already exists!");

        this.name = name;
        EXISTING.put(name, new WeakReference<Country>(this));
    }
}

不过,这可能有点矫枉过正。其中任何一个都可以与您的主要方法一起使用。它在 Ideone 中工作:http://ideone.com/uhDCzM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多