Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4075678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:19:44+00:00 2026-05-20T17:19:44+00:00

after searched for a while I still couldn’t find any answer to my question,

  • 0

after searched for a while I still couldn’t find any answer to my question, even there’s couple of Generics related topic, so here you go:

ArrayList<? super IOException> list = new ArrayList<Exception>();
list.add(new FileNotFoundException("this is ok."));
list.add(new IOException("This is ok"));
list.add(new ClassCastException("compile err"));//why compile err?
list.add(new Exception("compile err"));//why compile err? 

Why last two line doesn’t compile? Especially the last line. I’ve been doing quite a bit test on this topic but still couldn’t catch the logic.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:19:45+00:00Added an answer on May 20, 2026 at 5:19 pm

    ArrayList<? super IOException> could be any of the following (since there is a wild card):

    ArrayList<IOException>
    ArrayList<Exception>
    ArrayList<Throwable>
    ArrayList<Object>
    

    The code needs to work with all four possibilities.

    But if it is an ArrayList<IOException>, you cannot put in a ClassCastException or an Exception, hence the compile errors.

    Here’s what I don’t get: why this one compile——>>> list.add(new FileNotFoundException(“this is ok.”)); <<<—-I think FileNotFoundException also below the bound of IOException. but it compiles fine.

    No, FileNotFoundException is fine, because it extends IOException and you can put it in all of the four types of lists.

    Note that for historical reasons, arrays do not get the same strict type checking, you can compile the following (and then get an array store exception at runtime):

       Exception[] a = new IOException[4];
       a[0] = new FileNotFoundException("this is ok.");  
       a[1] = new IOException("This is ok");
       a[2] = new ClassCastException("compiles, but boom!");
       a[3] = new Exception("compiles, but boom!"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I'm still
I've searched other questions and, while this problem seems similar to a couple of
After reading this question , I was reminded of when I was taught Java
I have a UISearchDisplayController that displays some cells after a user has searched. After
I've searched and read (not just on SO) but can't seem to find a
I cannot find a way to set the text alignment in a QTabWidget. After
Greetings! This is my first question. I've searched all over the web as well
I've searched and can find things that almost seem like they would work but
After reading the Head First Design Patterns book and using a number of other
After the suggestion to use a library for my ajax needs I am going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.