VB.Net Tutorial/Development/Throw

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

Throw ApplicationException

Module Module1
    Sub Main()
        Try
            Throw New ApplicationException("You threw this custom exception.")
        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally
            Console.WriteLine("Finally...")
        End Try
    End Sub
End Module
You threw this custom exception.
Finally...

Throw custom exception

public class Test
   public Shared Sub Main
        Throw New MyException("Unhandled Error in MyMethod")
        
        
   End Sub
End class

Public Class MyException
    Inherits System.ApplicationException
    Private m_Source As String
    Public Sub New(ByVal Message As String)
        MyBase.New(Message)
        Me.m_Source = "ClassLibrary1"
    End Sub
End Class
Unhandled Exception: MyException: Unhandled Error in MyMethod
   at Test.Main()

Throw Exception Catch Rethrow

Public Class Tester
    Public Shared Sub Main
      Try
         ThrowExceptionCatchRethrow()
      Catch
         Console.WriteLine("Caught exception from ThrowExceptionCatchRethrow in Main")
      End Try
    End Sub
   Public Shared Sub ThrowExceptionCatchRethrow()
      Try
         Console.WriteLine("In ThrowExceptionCatchRethrow")
         Throw New Exception("Exception in ThrowExceptionCatchRethrow")
      Catch exceptionParameter As Exception
         Console.WriteLine("Message: " & exceptionParameter.Message)
         Throw exceptionParameter
      Finally
         Console.WriteLine("Finally executed in ThrowException")
      End Try

      Console.WriteLine("End of ThrowExceptionCatchRethrow")
   End Sub

End Class
In ThrowExceptionCatchRethrow
Message: Exception in ThrowExceptionCatchRethrow
Finally executed in ThrowException
Caught exception from ThrowExceptionCatchRethrow in Main

Throw Exception with catch

Public Class Tester
    Public Shared Sub Main
      Try
         Throw New Exception("Exception in ThrowExceptionWithCatch")
      Catch exceptionParameter As Exception
         Console.WriteLine("Message: " & exceptionParameter.Message)
      Finally
         Console.WriteLine("Finally executed in ThrowExceptionWithCatch")
      End Try
      Console.WriteLine("End of ThrowExceptionWithCatch")
    End Sub

End Class
Message: Exception in ThrowExceptionWithCatch
Finally executed in ThrowExceptionWithCatch
End of ThrowExceptionWithCatch

Throw Exception Without Catch

Public Class Tester
    Public Shared Sub Main
      Try
         Console.WriteLine("In ThrowExceptionWithoutCatch")
         Throw New Exception("Exception in ThrowExceptionWithoutCatch")
      Finally
         Console.WriteLine("Finally executed in ThrowExceptionWithoutCatch")
      End Try
    End Sub

End Class
In ThrowExceptionWithoutCatch
Unhandled Exception: System.Exception: Exception in ThrowExceptionWithoutCatch
   at Tester.Main()
Finally executed in ThrowExceptionWithoutCatch