VB.Net Tutorial/Development/Throw — различия между версиями
Admin (обсуждение | вклад) м (1 версия)  | 
				|
(нет различий) 
 | |
Версия 16:40, 26 мая 2010
Содержание
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 ModuleYou 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 ClassUnhandled 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 ClassIn 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 ClassMessage: 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 ClassIn ThrowExceptionWithoutCatch Unhandled Exception: System.Exception: Exception in ThrowExceptionWithoutCatch at Tester.Main() Finally executed in ThrowExceptionWithoutCatch