梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
ArgumentNullException Thrown if you call a method where one of the arguments is
a Nothing value。 This could be because you are passing the
Nothing value to the method or one of the arguments has a
Nothing value。
ArgumentOutOfRangeException Thrown if you call a method where one of the arguments is
not in the expected range。 While this exception sounds similar
to ArgumentException�察�it is more specialized and targets
whether an argument is in an acceptable range。 Check the
documentation of the method or the method implementation
on what the acceptable range is。 If you throw this exception�察 �
note the valid range in the error message。
ArithmeticException Thrown if a mathematical error is generated。
DivideByZeroException Thrown if you attempt to divide by zero。
FormatException Thrown if the format of the parameter is not correct。 For
example�察�if a method expects a number to be formatted with
a period and you use a ma�察�an exception is generated。
IndexOutOfRangeException Thrown if you attempt to reference an index of an array that
would be beyond the limits of the array。 This exception is
thrown if you have not allocated an array and then attempt to
reference an element�察�or if you attempt to reference a nega
tive index of the array。
InsufficientMemoryException Thrown if not enough memory is available。 Although this
exception is not generated often�察�it can be generated if you
attempt to allocate an array when you specify something
along the lines of 5 trillion elements ��due to an improperly
assigned array size variable��。
InvalidCastException Thrown if you attempt to cast one type to another type that is
not supported。 This exception is very mon when you use
inheritance and attempt a cast。
NotImplementedException Thrown when using methods or properties without an imple
mentation。 Often�察�you don¨t have time to implement all of the
code at once。 For those properties or methods that have not
been implemented�察�don¨t leave an empty property or method
implementation。 Instead�察�throw an exception。 Then you will
know if you have forgotten to implement something。
´´´´´´´´´´´´´´´´´´´´´´Page 151´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 5 * L E AR N IN G AB O U T V I SU A L B AS IC E X CE PT I ON HA N D L IN G 129
Table 5´1。 mon Exception Types
Exception Description
NotSupportedException Thrown when you attempt to use an interface instance and a
method that cannot work。 For example�察�if you open a read´write
buffer to a read´only CD´ROM�察�you will get this exception
when writing to the CD´ROM。 If you attempt to read from the
interface instance�察�an exception will not be thrown。
NullReferenceException Thrown when you attempt to call a method or property of a
variable that has not been assigned with a valid type instance。
OutOfMemoryException Similar to InsufficientMemoryException。
OverflowException Thrown when you attempt to perform numeric operations
that are not supported�察�such as adding 2 billion to 2 billion
using a 32´bit integer。
SystemException Thrown by the operating system。 Do not attempt to derive
from this class。
Writing Exception´Safe Code
Now that you¨ve seen how to implement exception handlers�察�we¨ll look at an even better approach
to exceptions�此�not generating them。 We¨ll focus on how you can make your code safer and less
likely to generate exceptions。
Writing Defensive Code
All too often�察�developers get exceptions such as NullReferenceException because they didn¨t
make sure that the state in a piece of code was valid。 And if the state is not valid�察�an exception
will occur。 In fact�察�in this chapter�察�one of the previous examples has just such a situation�察�which
you may have noticed。 Here is the code that has a dumb little exception possibility�此�
Sub TestCallingExample�┌�
Dim cls As CallingExample = Nothing
Try
cls = New CallingExample�┌�
cls。Method�┌�
Catch ex As Exception
End Try
Console。WriteLine�─�Depth is �──�& cls。GetDepth�┌� & ;��;��
End Sub
The problem is in the bolded line�察�which assumes that cls will always reference a valid
instance of CallingExample。 This is an assumption that you cannot afford to make。 If an excep
tion occurred while instantiating CallingExample�察�cls will still be Nothing�察�and the Catch block
will catch the instantiation exception�察�saving a program crash。 However�察�the use of cls。GetDepth�┌�
immediately after that throws your protection out the window�察�as cls is Nothing and will
generate a NullReferenceException。 Here¨s a better way to write this code�此�
´´´´´´´´´´´´´´´´´´´´´´Page 152´´´´´´´´´´´´´´´´´´´´´´´
130 CH AP T E R 5 * L E A R N IN G AB OU T V I SU A L B AS IC E X C E P TI ON H AN D L IN G
Sub TestCallingExample�┌�
Dim cls As CallingExample = Nothing
Try
cls = New CallingExample�┌�
cls。Method�┌�
Catch ex As Exception
End Try
If cls IsNot Nothing Then
Console。WriteLine�─�Depth is �──�& cls。GetDepth�┌� & ;��;��
End If
End Sub
The bolded code illustrates defensive coding that tests if cls is not Nothing�察�and if so�察�then
allows the referencing of cls。GetDepth�┌�。 Now the code is exception´safe。 It does not mean
exceptions cannot occur�察�because calling GetDepth�┌� could still generate an exception inter
nally�察�but with respect to the TestCallingExample�┌� method�察�you have made everything as safe
as possible�察�and have assumed GetDepth�┌� is a low exception´risk method。
Missing from the TestCallingE