当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Exception构造函数代码示例

本文整理汇总了VB.NET中System.Exception.Exception构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET Exception构造函数的具体用法?VB.NET Exception怎么用?VB.NET Exception使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在System.Exception的用法示例。


在下文中一共展示了Exception构造函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: New

' Example for the Exception( ) constructor.
Namespace NDP_UE_VB

    ' Derive an exception with a predefined message.
    Class NotEvenException
        Inherits Exception
           
        Public Sub New( )
            MyBase.New( _
                "The argument to a function requiring " & _
                "even input is not divisible by 2." )
        End Sub
    End Class

    Module NewExceptionDemo
       
        Sub Main( )
            Console.WriteLine( _
                "This example of the Exception( ) constructor " & _
                "generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of the base class." & _
                vbCrLf )

            CalcHalf( 12 )
            CalcHalf( 15 )
              
            Console.WriteLine(vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of a derived class." & _
                vbCrLf )

            CalcHalf2( 24 )
            CalcHalf2( 27 )
        End Sub
           
        ' Half throws a base exception if the input is not even.
        Function Half( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New Exception( )
            Else
                Return input / 2
            End If
        End Function ' Half
            
        ' Half2 throws a derived exception if the input is not even.
        Function Half2( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New NotEvenException( )
            Else
                Return input / 2
            End If
        End Function ' Half2
            
        ' CalcHalf calls Half and catches any thrown exceptions.
        Sub CalcHalf( input As Integer )

            Try
                Dim halfInput As Integer = Half( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub
           
        ' CalcHalf2 calls Half2 and catches any thrown exceptions.
        Sub CalcHalf2( input As Integer )

            Try
                Dim halfInput As Integer = Half2( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub

    End Module ' NewExceptionDemo
End Namespace ' NDP_UE_VB
开发者ID:VB.NET开发者,项目名称:System,代码行数:85,代码来源:Exception

输出:

Here, an exception is thrown using the
parameterless constructor of the base class.

Half of 12 is 6.
System.Exception: Exception of type System.Exception was thrown.
at NDP_UE_VB.NewExceptionDemo.Half(Int32 input)
at NDP_UE_VB.NewExceptionDemo.CalcHalf(Int32 input)

Here, an exception is thrown using the
parameterless constructor of a derived class.

Half of 24 is 12.
NDP_UE_VB.NotEvenException: The argument to a function requiring even input i
s not divisible by 2.
at NDP_UE_VB.NewExceptionDemo.Half2(Int32 input)
at NDP_UE_VB.NewExceptionDemo.CalcHalf2(Int32 input)

示例2: New

' Example for the Exception( String ) constructor( String ).
Namespace NDP_UE_VB

    ' Derive an exception with a specifiable message.
    Class NotEvenException
        Inherits Exception

        Private Const notEvenMessage As String = _
            "The argument to a function requiring " & _
            "even input is not divisible by 2."
           
        Public Sub New()
            MyBase.New(notEvenMessage)
        End Sub
           
        Public Sub New(auxMessage As String)
            MyBase.New(String.Format("{0} - {1}", _
                auxMessage, notEvenMessage))
        End Sub
    End Class

    Module NewSExceptionDemo
       
        Sub Main()
            Console.WriteLine( _
                "This example of the Exception( String )" & vbCrLf & _
                "constructor generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "constructor of the base class." & vbCrLf )

            CalcHalf(18)
            CalcHalf(21)
              
            Console.WriteLine(vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "constructor of a derived class." & vbCrLf )

            CalcHalf2(30)
            CalcHalf2(33)
        End Sub
           
        ' Half throws a base exception if the input is not even.
        Function Half(input As Integer) As Integer

            If input Mod 2 <> 0 Then
                Throw New Exception( String.Format( _
                    "The argument {0} is not divisible by 2.", _
                    input ) )
            Else
                Return input / 2
            End If
        End Function ' Half
            
        ' Half2 throws a derived exception if the input is not even.
        Function Half2(input As Integer) As Integer

            If input Mod 2 <> 0 Then
                Throw New NotEvenException( _
                    String.Format( "Invalid argument: {0}", input ) )
            Else
                Return input / 2
            End If
        End Function ' Half2
            
        ' CalcHalf calls Half and catches any thrown exceptions.
        Sub CalcHalf(input As Integer)

            Try
                Dim halfInput As Integer = Half(input)
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub
           
           
        ' CalcHalf2 calls Half2 and catches any thrown exceptions.
        Sub CalcHalf2( input As Integer )

            Try
                Dim halfInput As Integer = Half2( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub

    End Module ' NewSExceptionDemo
End Namespace ' NDP_UE_VB

' This example of the Exception( String )
开发者ID:VB.NET开发者,项目名称:System,代码行数:96,代码来源:Exception

输出:

Here, an exception is thrown using the
constructor of the base class.

Half of 18 is 9.
System.Exception: The argument 21 is not divisible by 2.
at NDP_UE_VB.NewSExceptionDemo.Half(Int32 input)
at NDP_UE_VB.NewSExceptionDemo.CalcHalf(Int32 input)

Here, an exception is thrown using the
constructor of a derived class.

Half of 30 is 15.
NDP_UE_VB.NotEvenException: Invalid argument: 33 - The argument to a function
requiring even input is not divisible by 2.
at NDP_UE_VB.NewSExceptionDemo.Half2(Int32 input)
at NDP_UE_VB.NewSExceptionDemo.CalcHalf2(Int32 input)

示例3: New

' If compiling with the Visual Basic compiler (vbc.exe) from the command
' prompt, be sure to add the following switch:
'    /reference:System.Runtime.Serialization.Formatters.Soap.dll 
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Security.Permissions

 ' Define a serializable derived exception class.
 <Serializable()>  _
 Class SecondLevelException
     Inherits Exception

     ' This public constructor is used by class instantiators.
     Public Sub New( message As String, inner As Exception )
         MyBase.New( message, inner )

         HelpLink = "http://MSDN.Microsoft.com"
         Source = "Exception_Class_Samples"
     End Sub

     ' This protected constructor is used for deserialization.
     Protected Sub New( info As SerializationInfo, _
         context As StreamingContext )
             MyBase.New( info, context )
     End Sub

     ' GetObjectData performs a custom serialization.
     <SecurityPermissionAttribute(SecurityAction.Demand, _
                                  SerializationFormatter:=True)> _
     Overrides Sub GetObjectData( info As SerializationInfo, _
         context As StreamingContext)

         ' Change the case of two properties, and then use the
         ' method of the base class.
         HelpLink = HelpLink.ToLower()
         Source = Source.ToUpperInvariant()

         MyBase.GetObjectData(info, context)
     End Sub
 End Class

 Module SerializationDemo

     Sub Main()
         Console.WriteLine( _
             "This example of the Exception constructor " & _
             "and Exception.GetObjectData " & vbCrLf & _
             "with SerializationInfo and StreamingContext " & _
             "parameters generates " & vbCrLf & _
             "the following output." & vbCrLf )

         ' This code forces a division by 0 and catches the
         ' resulting exception.
         Try
             Try
                 Dim zero As Integer = 0
                 Dim ecks As Integer = 1 \ zero

             ' Create a new exception to throw again.
             Catch ex As Exception

                 Dim newExcept As New SecondLevelException( _
                     "Forced a division by 0 and threw " & _
                     "another exception.", ex )

                 Console.WriteLine( _
                     "Forced a division by 0, caught the " & _
                     "resulting exception, " & vbCrLf & _
                     "and created a derived exception:" & vbCrLf )
                 Console.WriteLine( "HelpLink: {0}", _
                     newExcept.HelpLink )
                 Console.WriteLine( "Source:   {0}", _
                     newExcept.Source )

                 ' This FileStream is used for the serialization.
                 Dim stream As New FileStream( _
                     "NewException.dat", FileMode.Create )

                 ' Serialize the derived exception.
                 Try
                     Dim formatter As New SoapFormatter( Nothing, _
                         New StreamingContext( _
                             StreamingContextStates.File ) )
                     formatter.Serialize( stream, newExcept )

                     ' Rewind the stream and deserialize the
                     ' exception.
                     stream.Position = 0
                     Dim deserExcept As SecondLevelException = _
                         CType( formatter.Deserialize( stream ), _
                             SecondLevelException )

                     Console.WriteLine( vbCrLf & _
                         "Serialized the exception, and then " & _
                         "deserialized the resulting stream " & _
                         "into a " & vbCrLf & "new exception. " & _
                         "The deserialization changed the case " & _
                         "of certain properties:" & vbCrLf )

                     ' Throw the deserialized exception again.
                     Throw deserExcept

                 Catch se As SerializationException
                     Console.WriteLine( "Failed to serialize: {0}", _
                         se.ToString( ) )

                 Finally
                     stream.Close( )
                 End Try
             End Try
         Catch ex As Exception
             Console.WriteLine( "HelpLink: {0}", ex.HelpLink )
             Console.WriteLine( "Source:   {0}", ex.Source )

             Console.WriteLine( )
             Console.WriteLine( ex.ToString( ) )
         End Try
     End Sub
 End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:120,代码来源:Exception

输出:

Forced a division by 0, caught the resulting exception,
and created a derived exception:

HelpLink: http://MSDN.Microsoft.com
Source:   Exception_Class_Samples

Serialized the exception, and then deserialized the resulting stream into a
new exception. The deserialization changed the case of certain properties:

HelpLink: http://msdn.microsoft.com
Source:   EXCEPTION_CLASS_SAMPLES

NDP_UE_VB.SecondLevelException: Forced a division by 0 and threw another exce
ption. ---> System.DivideByZeroException: Attempted to divide by zero.
at NDP_UE_VB.SerializationDemo.Main()
--- End of inner exception stack trace ---
at NDP_UE_VB.SerializationDemo.Main()

示例4: New

' Sample for Exception( String, Exception ) constructor.
Namespace NDP_UE_VB

    ' Derive an exception with a specifiable message and inner exception.
    Class LogTableOverflowException
        Inherits Exception

        Private Const overflowMessage As String = _
            "The log table has overflowed."
           
        Public Sub New( )
            MyBase.New( overflowMessage )
        End Sub
           
        Public Sub New( auxMessage As String )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ) )
        End Sub
           
        Public Sub New( auxMessage As String, inner As Exception )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ), inner )
        End Sub
    End Class

    Class LogTable
       
        Public Sub New( numElements As Integer )
            logArea = New String( numElements ) { }
            elemInUse = 0
        End Sub
           
        Protected logArea( ) As String
        Protected elemInUse As Integer
           
        ' The AddRecord method throws a derived exception 
        ' if the array bounds exception is caught.
        Public Function AddRecord( newRecord As String ) As Integer

            Try
                Dim curElement as Integer = elemInUse
                logArea( elemInUse ) = newRecord
                elemInUse += 1
                Return curElement

            Catch ex As Exception
                Throw New LogTableOverflowException( String.Format( _
                    "Record ""{0}"" was not logged.", newRecord ), ex )
            End Try
        End Function ' AddRecord
        End Class

        Module OverflowDemo
           
        ' Create a log table and force an overflow.
        Sub Main()
            Dim log As New LogTable(4)
              
            Console.WriteLine( _
                "This example of the Exception( String, Exception )" & _
                vbCrLf & "constructor generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Example of a derived exception " & vbCrLf & _
                "that references an inner exception:" & vbCrLf )
            Try
                Dim count As Integer = 0
                 
                Do
                    log.AddRecord( _
                        String.Format( _
                            "Log record number {0}", count ) )
                    count += 1
                Loop

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub

    End Module ' OverflowDemo
End Namespace ' NDP_UE_VB

' This example of the Exception( String, Exception )
开发者ID:VB.NET开发者,项目名称:System,代码行数:83,代码来源:Exception

输出:

Example of a derived exception
that references an inner exception:

NDP_UE_VB.LogTableOverflowException: The log table has overflowed. - Record "
Log record number 5" was not logged. ---> System.IndexOutOfRangeException: In
dex was outside the bounds of the array.
at NDP_UE_VB.LogTable.AddRecord(String newRecord)
--- End of inner exception stack trace ---
at NDP_UE_VB.LogTable.AddRecord(String newRecord)
at NDP_UE_VB.OverflowDemo.Main()


注:本文中的System.Exception.Exception构造函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。