简体   繁体   English

找不到符号IllegalArgumentException

[英]Cannot find symbol IllegalArgumentException

I have a very simple program that tries to throw an exception. 我有一个非常简单的程序试图抛出异常。 The compiler says it cannot find IllegalArgumentException even though it didm't say anything about that name when I put it in the throw specifier part: 编译器说它无法找到IllegalArgumentException即使我把它放在throw说明符部分时它没有说出任何关于该名称的内容:

import java.lang.*;

class A
{
    public A() throws IllegalArgumentException
    {
        if (n <= 0)
            throw IllegalArgumentException("n is less than 0");
    }
}

Here's the error: 这是错误:

Main.java:28: error: cannot find symbol
            throw IllegalArgumentException("n is less than 0");
                  ^
  symbol:   method IllegalArgumentException(String)
  location: class A
1 error

I realize this is very simple (my very first attempt at writing Java). 我意识到这很简单(我第一次尝试编写Java)。 I've tried looking for answers but they haven't helped me to a solution. 我试过寻找答案,但他们没有帮我解决问题。

Use the new keyword 使用new关键字

public A() {
    int n = ...;
    if (n <= 0) {
        throw new IllegalArgumentException("n is less than 0");
    }
}

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

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