Runtime.CompilerServices
Public Class EntryPoint
Shared Sub Main()
Try
Dim list As ArrayList = New ArrayList()
CHAPTER 8 n EXCEPTION HANDLING 134
list.Add(1)
Console.WriteLine("Item 10 = {0}", list(10))
Catch x As ArgumentOutOfRangeException
Console.WriteLine("=== ArgumentOutOfRangeException Handler ===")
Console.WriteLine(x)
Console.WriteLine("=== ArgumentOutOfRangeException Handler ===")
Catch x As Exception
Console.WriteLine("=== Exception Handler ===")
Console.WriteLine(x)
Console.WriteLine("=== Exception Handler ===")
Finally
Console.WriteLine(vbCrLf & "Cleaning up . . .")
End Try
End Sub
End Class
Running the preceding code displays the following output:
=== ArgumentOutOfRangeException Handler ===
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative
and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at Exception_Handling1.EntryPoint.Main() in C:\Apress\AVB 2008\EH\EH.vb:line 11
=== ArgumentOutOfRangeException Handler ===
Cleaning up . . .
Once you see the code in the Try block, you know it is destined to throw an
ArgumentOutOfRange exception, as we are trying to display the 10th element of a 1 element
ArrayList to the console.
Pages:
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232