VB.Net Tutorial/Development/Throw — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:53, 26 мая 2010

Throw ApplicationException

<source lang="vbnet">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</source>

You threw this custom exception.
Finally...

Throw custom exception

<source lang="vbnet">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</source>

Unhandled Exception: MyException: Unhandled Error in MyMethod
   at Test.Main()

Throw Exception Catch Rethrow

<source lang="vbnet">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</source>

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

Throw Exception with catch

<source lang="vbnet">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</source>

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

Throw Exception Without Catch

<source lang="vbnet">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</source>

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