按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
Try
Throw New Exception(〃Exception in action 2。4。〃)
Catch thrown As Exception
Throw New Exception(〃Exception in action 2 has been caught。〃; thrown)
End Try
…………………………………………………………Page 144……………………………………………………………
122 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
The first variation; Exception(〃Exception in action 2。4。〃); uses the string description
constructor parameter that passes text describing what went wrong。 The text is meant for human
understanding; so don’t use text like “Error 168: something went wrong。” The second variation;
Exception(〃Exception in action 2 has been caught。〃; thrown); includes the original excep
tion as an additional constructor parameter in a newly thrown exception。 This allows you to
pass on even more information。
The generated output of this code looks like this:
Unhandled Exception: System。Exception: Exception in action 2 has been caught。
………》 System。Exception: Exception in action 2。4。
The generated exception tells you clearly where the exception occurred and where it was
processed。 You have a plete flow of the actions。
■Note An experienced developer may say that the flow is also presented by the stack that is dumped by
the program if the exception is not caught。 Yes; you could see the program flow based on this stack dump;
but deciphering a stack dump where you have a stack of say 10 or 15 is not much fun to figure out or read。
Consider the following amendment to the code; which reduces the amount of information。
Try
Throw New Exception(〃Exception in action 2。4。〃)
Catch exception1 As Exception
Throw New Exception(〃Exception in action 2 has been caught〃)
End Try
The results are not as enlightening:
Unhandled Exception: System。Exception: Exception in action 2 has been caught。
If you want to gain access to the error string; you can use the Message property of an exception。
Try
Throw New Exception(〃Exception in action 2。4。〃)
Catch thrown As Exception
Console。WriteLine(thrown。Message)
Throw New Exception(〃Exception in action 2 has been caught。〃)
End Try
You still have the more specific message; but not as part of the flow of exceptions:
…………………………………………………………Page 145……………………………………………………………
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 123
Exception in action 2。4。
Unhandled Exception: System。Exception: Exception in action 2 has been caught。
DON’T REPEAT ERROR MESSAGES
When you throw exceptions; make sure that you don’t use the same error message twice。 Imagine the situa
tion where you deliver a program into production and the error message “File not found” is generated。 If this
text is used in multiple places; when the user calls tech support; the support staff will have no idea which file
was not found。 Instead; in the error message; tell the user which file was not found and why。 The more details
you deliver; the easier it is for the support desk to help users get around the problem。
If you do need to use the same text in multiple places; add a context identifier。 For example; you could
generate a load error fault in the context of loading a file using a dialog box。 Or you could generate a load error in
the context of loading a file based on a mand…line argument。 Specify each context as an additional piece
of information appended or prefixed to the exception text; much like the previous code illustrated when catching an
exception in action 2。
Safeguarding Against Stack Unwinding
Exception handling makes it simple for you to stop your program from crashing; but it does not
help you from ensuring that the state of your application is still intact。 Consider the example
shown in Figure 5…5; which illustrates how program state can be corrupted due to an exception
that is caught and swallowed。
Class MyType
Public DataMember As Integer
End Class
Class Tests LocalDataMember is assigned
Public LocalDataMember As Integer
Public Sub GeneratesException()
Me。LocalDataMember = 10 DataMember should also be assigned;
Dim cls As MyType = Nothing but an exception is raised; and the
cls。DataMember = 10
End Sub assignment doesn’t happen
Public Sub RunAll()
Console。WriteLine(〃LocalDataMember=〃 & LocalDataMember)
Try At this point; neither
GeneratesException() member is assigned
Catch exception1 As Exception
End Try
Console。WriteLine(〃LocalDataMember=〃 & LocalDataMember) At this point; DataMember is not
End Sub assigned; but LocalDataMember is
End Class
assigned; indicating a corrupt state
Figure 5…5。 Exceptions can corrupt the state of a program。
When an exception is caught; the stack is unwound。 Consider the example shown in
Figure 5…6; where the unwinding of the stack can have the side effect of jumping over a method call。
…………………………………………………………Page 146……………………………………