0

So, Im trying to run this code:

public class MyClass {
    public static void main(String[] args) {
        java.util.List<? extends Exception> myList=null;
        Exception exc1 = null;
        Exception exc2 = null;
        myList.add(exc2);
        exc1=myList.iterator().next();
    }
}

I am aware this code is written poorly, but this is a code from a test I saw. this code doesn't compile because of the add line, and the compilation error is:

The method add(capture#1-of ? extends Exception) in the type List<capture#1-of ? extends Exception> is not applicable for the arguments (Exception)

I don't get why I can't this error pops up. I really you can explain it to me. Even if I change exc2 static type to RuntimeException, it doesn't work.

thanks.

0 Answers0