VB.Net/Language Basics/Catch Exception — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:42, 26 мая 2010
Catch All Unhandled Exceptions
<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.IO Public Class MainClass
Shared Sub Main() AddHandler Application.ThreadException, AddressOf app_ThreadException Application.OnThreadException(New InvalidDataException("There")) Throw New InvalidDataException("Here")
End Sub " Catch a ThreadException event. Shared Private Sub app_ThreadException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs) Console.WriteLine("Caught unhandled exception") End Sub
End Class
</source>
Catch Exception Demo
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim x As Int32 = 100 Dim y As Int32 = 0 Dim k As Int32 Try k = x / y Catch e As Exception Console.WriteLine(e.Message) End Try End Sub
End Class
</source>
Try and catch Exception: InvalidCastException
<source lang="vbnet"> Imports System Imports System.Diagnostics Public Class MainClass
Shared Sub Main() Try Dim i As Integer = CInt("bad value") Catch ex As InvalidCastException Dim txt As String = "InvalidCastException" MsgBox(txt) Catch ex As Exception Dim txt As String = "Exception" MsgBox(txt) End Try End Sub
End Class
</source>