VB.Net/Language Basics/Catch Exception

Материал из VB Эксперт
Версия от 12:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Catch All Unhandled Exceptions

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


Catch Exception Demo

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


Try and catch Exception: InvalidCastException

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