How to determine which exceptions "x []" can be thrown?

The C # reference for square brackets says: Square brackets [] are used for arrays, indexes, and attributes. They can also be used with pointers. (He also says that for arrays) An exception is thrown if the array index is out of range.

So, when you use square brackets for something other than an array, how do you know which exceptions can be thrown?

For a dictionary (for example), if you use access methods like Dictionary.TryGetValue , you can easily see exceptions that throw can, but the C # link for square brackets [] only says that it can throw an exception for an index out of range on arrays.

So, if you want to use square brackets for some data type, where you can search, what exceptions can be selected for this data type?

I tried to contact a non-existent member of the Dictionary, just to find out what would happen, and I got a KeyNotFoundException. I know that you can also get a NullReferenceException. Where is this documented? And what is the complete list?

+5
source share
4 answers

It is documented with each implementation. There is no exhaustive list, because the operator can be overloaded, so any exception can be thrown. An operator is usually documented as a property Item.

Here are some specific usage documents:

Dictionary.Item

Array.Item

, . , , : - (, TryParse, ), , , .

. , NullReferenceException, , , , ( NullReferenceException).

Catching generic Exception , , .

+4

[] < TKey, TValue > List <T> , indexer. Item.

+4

- , .

, DataRow, ..

+1
source

When used to access an array, it can call IndexOutOfRangeException.

When used for an attribute, it cannot raise any exceptions at all, since any error will occur at compile time.

When used to access an index, it can raise any exception. An indexer can be implemented in any way you need and throw any exception.

+1
source

All Articles