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

  • Home
  • SEARCH
  • 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 8736639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:21:02+00:00 2026-06-13T10:21:02+00:00

Consider the following MCVE : public class MyClass { private LinkedList<Foo> myList = new

  • 0

Consider the following MCVE:

public class MyClass {
    private LinkedList<Foo> myList = new LinkedList<Foo>();

    // Some irrevelant stuff which includes loading myList

    public void myMethod() {
        LinkedList<Foo> newList;

        try {
             newList = (LinkedList<Foo>) myList.clone();
        } catch (ClassCastException e) {
             // do something in case java screws up
        }
    }
}

I know that you can get rid of the warning by using @SuppressWarnings("unchecked") but why doesn’t the try/catch block work? Is it a waste of time and energy to put the try/catch in there?

  • 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-06-13T10:21:03+00:00Added an answer on June 13, 2026 at 10:21 am

    That won’t work, because you are not getting a ClassCastException for this.

    The erased type of the list cannot be checked at runtime.

    You might get a ClassCastException when you try to get something out of the List (and that turns out not to be a Foo), but the List itself is just a LinkedList (without knowing what its element types can be).

    An instance of LinkedList<Foo> looks exactly like a LinkedList<Bar> at runtime.

    The reason for the warning is that the runtime system cannot guarantee that the cast you are doing is correct (it can check the LinkedList part, but not the generic type).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following code - namespace N1 { class A { //some implementation } class
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Consider following class public class AccountGroup : Entity<int> { public AccountGroup() { Accounts =
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some

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.