Cannot find symbol IllegalArgumentException

I have a very simple program trying to throw an exception. The compiler says it can't find it IllegalArgumentException

, although it didn't say anything about that name when I put it in the throw part of the specifier:

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 first attempt at writing Java). I tried looking for answers but they didn’t work for me in the solution.

+3


source to share


1 answer


Use a new keyword



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

      

+7


source







All Articles