ArrayList.Add unclear on error handling

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hi,

ArrayList.Add(...) method doesnt indicate what value we can check for
error.

NotSupportedException is the only exception but thats for ReadOnly or
FixedSize lists. Will the return value be negative? The documentation is
unclear in MSDN on this.

If .Add() fails, would it return -1? If its variable size Im sure Ill get
an OutOfMemoryException before but there isnt a specific error for .Add
failure


Fanks.
 
Hi,
I guess you got that one right. If during the execution of the Add method,
there is not enough space for the arraylist to expand, you are going to get
an OutOfMemory exception. I do not think that the function call will
complete for you get a return value( -1 or otherwise ).
My guess is that you will need to Add that logic into your own code for you
to throw a custom exception if you are particular about handling .Add
failures.

-Diws
 
news.microsoft.com said:
ArrayList.Add(...) method doesnt indicate what value we can check for
error.

You don't - you get an exception if something goes wrong.
NotSupportedException is the only exception but thats for ReadOnly or
FixedSize lists. Will the return value be negative? The documentation is
unclear in MSDN on this.

If .Add() fails, would it return -1? If its variable size Im sure Ill get
an OutOfMemoryException before but there isnt a specific error for .Add
failure

What kind of failure would there be other than some sort of internal
logic error which would be best indicated by an exception?
 
What exception? MSDN only lists NotSupportedException for this type.

That was my question. So I will get a general exception, like
OutOfMemoryException (for variable size ArrayLists) and not a specific
ArrayList exception. But according to MSDN this isnt thrown by ArrayList,
so its coming from somewhere else.
 
news.microsoft.com said:
What exception? MSDN only lists NotSupportedException for this type.

That was my question. So I will get a general exception, like
OutOfMemoryException (for variable size ArrayLists) and not a specific
ArrayList exception. But according to MSDN this isnt thrown by ArrayList,
so its coming from somewhere else.

Yes - it'll be thrown by whatever ArrayList uses internally, but it's
not the kind of thing you should be catching specifically anyway. If
ArrayList.Add fails (and not because it's readonly or a fixed size),
chances are the execution environment is in a sufficiently nasty state
that whatever you're doing is *not* going to recover. Such extreme
situations don't merit individual exceptions for every possible
operation which could provoke them.
 
Back
Top