• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET Exception类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: ExceptionTestClass

Class ExceptionTestClass
   
   Public Shared Sub Main()
      Dim x As Integer = 0
      Try
         Dim y As Integer = 100 / x
      Catch e As ArithmeticException
         Console.WriteLine("ArithmeticException Handler: {0}", e.ToString())
      Catch e As Exception
         Console.WriteLine("Generic Exception Handler: {0}", e.ToString())
      End Try
   End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:13,代码来源:Exception

输出:

ArithmeticException Handler: System.OverflowException: Arithmetic operation resulted in an overflow.
at ExceptionTestClass.Main()


示例2: Person

Public Class Person
   Private _name As String
   
   Public Property Name As String
      Get
         Return _name
      End Get
      Set
         _name = value
      End Set
   End Property
   
   Public Overrides Function Equals(obj As Object) As Boolean
      ' This implementation contains an error in program logic:
      ' It assumes that the obj argument is not null.
      Dim p As Person = CType(obj, Person)
      Return Me.Name.Equals(p.Name)
   End Function
End Class

Module Example
   Public Sub Main()
      Dim p1 As New Person()
      p1.Name = "John"
      Dim p2 As Person = Nothing
      
      ' The following throws a NullReferenceException.
      Console.WriteLine("p1 = p2: {0}", p1.Equals(p2))   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:30,代码来源:Exception


示例3: Main

Public Class Person
   Private _name As String
   
   Public Property Name As String
      Get
         Return _name
      End Get
      Set
         _name = value
      End Set
   End Property
   
   Public Overrides Function Equals(obj As Object) As Boolean
      ' This implementation handles a null obj argument.
      Dim p As Person = TryCast(obj, Person)
      If p Is Nothing Then 
         Return False
      Else
         Return Me.Name.Equals(p.Name)
      End If
   End Function
End Class

Module Example
   Public Sub Main()
      Dim p1 As New Person()
      p1.Name = "John"
      Dim p2 As Person = Nothing
      
      Console.WriteLine("p1 = p2: {0}", p1.Equals(p2))   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:32,代码来源:Exception

输出:

p1 = p2: False


示例4: Library

' 导入命名空间
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

Public Module Library
   <Extension()>
   Public Function FindOccurrences(s As String, f As String) As Integer()
      Dim indexes As New List(Of Integer)
      Dim currentIndex As Integer = 0
      Try
         Do While currentIndex >= 0 And currentIndex < s.Length
            currentIndex = s.IndexOf(f, currentIndex)
            If currentIndex >= 0 Then
               indexes.Add(currentIndex)
               currentIndex += 1
            End If
         Loop
      Catch e As ArgumentNullException
         ' Perform some action here, such as logging this exception.
         
         Throw
      End Try
      Return indexes.ToArray()
   End Function
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:25,代码来源:Exception


示例5: Example

Module Example
   Public Sub Main()
      Dim s As String = "It was a cold day when..."
      Dim indexes() As Integer = s.FindOccurrences("a")
      ShowOccurrences(s, "a", indexes)
      Console.WriteLine()

      Dim toFind As String = Nothing
      Try
         indexes = s.FindOccurrences(toFind)
         ShowOccurrences(s, toFind, indexes)
      Catch e As ArgumentNullException
         Console.WriteLine("An exception ({0}) occurred.",
                           e.GetType().Name)
         Console.WriteLine("Message:{0}   {1}{0}", vbCrLf, e.Message)
         Console.WriteLine("Stack Trace:{0}   {1}{0}", vbCrLf, e.StackTrace)
      End Try
   End Sub
   
   Private Sub ShowOccurrences(s As String, toFind As String, indexes As Integer())
      Console.Write("'{0}' occurs at the following character positions: ",
                    toFind)
      For ctr As Integer = 0 To indexes.Length - 1
         Console.Write("{0}{1}", indexes(ctr),
                       If(ctr = indexes.Length - 1, "", ", "))
      Next
      Console.WriteLine()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:29,代码来源:Exception

输出:

a' occurs at the following character positions: 4, 7, 15

An exception (ArgumentNullException) occurred.
Message:
Value cannot be null.
Parameter name: value

Stack Trace:
at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
ngComparison comparisonType)
at Library.FindOccurrences(String s, String f)
at Example.Main()


示例6: ShowOccurrences

Try
   indexes = s.FindOccurrences(toFind)
   ShowOccurrences(s, toFind, indexes)
Catch e As ArgumentNullException
   Console.WriteLine("An exception ({0}) occurred.",
                     e.GetType().Name)
   Console.WriteLine("   Message: {1}{0}", vbCrLf, e.Message)
   Console.WriteLine("   Stack Trace:{0}   {1}{0}", vbCrLf, e.StackTrace)
   Dim ie As Exception = e.InnerException
   If ie IsNot Nothing Then
      Console.WriteLine("   The Inner Exception:")
      Console.WriteLine("      Exception Name: {0}", ie.GetType().Name)
      Console.WriteLine("      Message: {1}{0}", vbCrLf, ie.Message)
      Console.WriteLine("      Stack Trace:{0}   {1}{0}", vbCrLf, ie.StackTrace)
   End If
End Try
开发者ID:VB.NET开发者,项目名称:System,代码行数:16,代码来源:Exception

输出:

a' occurs at the following character positions: 4, 7, 15

An exception (ArgumentNullException) occurred.
Message: You must supply a search string.

Stack Trace:
at Library.FindOccurrences(String s, String f)
at Example.Main()

The Inner Exception:
Exception Name: ArgumentNullException
Message: Value cannot be null.
Parameter name: value

Stack Trace:
at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri
ngComparison comparisonType)
at Library.FindOccurrences(String s, String f)


示例7: New

' 导入命名空间
Imports System.Runtime.Serialization

<Serializable()> _
Public Class NotPrimeException : Inherits Exception
   Private notAPrime As Integer

   Protected Sub New()
      MyBase.New()
   End Sub

   Public Sub New(value As Integer)
      MyBase.New(String.Format("{0} is not a prime number.", value))
      notAPrime = value
   End Sub

   Public Sub New(value As Integer, message As String)
      MyBase.New(message)
      notAPrime = value
   End Sub

   Public Sub New(value As Integer, message As String, innerException As Exception)
      MyBase.New(message, innerException)
      notAPrime = value
   End Sub

   Protected Sub New(info As SerializationInfo,
                     context As StreamingContext)
      MyBase.New(info, context)
   End Sub

   Public ReadOnly Property NonPrime As Integer
      Get
         Return notAPrime
      End Get
   End Property
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:37,代码来源:Exception


示例8: New

' 导入命名空间
Imports System.Collections.Generic

<Serializable()> Public Class PrimeNumberGenerator
   Private Const START As Integer = 2
   Private maxUpperBound As Integer = 10000000
   Private upperBound As Integer
   Private primeTable() As Boolean
   Private primes As New List(Of Integer)

   Public Sub New(upperBound As Integer)
      If upperBound > maxUpperBound Then
         Dim message As String = String.Format(
             "{0} exceeds the maximum upper bound of {1}.",
             upperBound, maxUpperBound)
         Throw New ArgumentOutOfRangeException(message)
      End If
      Me.upperBound = upperBound
      ' Create array and mark 0, 1 as not prime (True).
      ReDim primeTable(upperBound)
      primeTable(0) = True
      primeTable(1) = True

      ' Use Sieve of Eratosthenes to determine prime numbers.
      For ctr As Integer = START To CInt(Math.Ceiling(Math.Sqrt(upperBound)))
         If primeTable(ctr) Then Continue For

         For multiplier As Integer = ctr To CInt(upperBound \ ctr)
            If ctr * multiplier <= upperBound Then primeTable(ctr * multiplier) = True
         Next
      Next
      ' Populate array with prime number information.
      Dim index As Integer = START
      Do While index <> -1
         index = Array.FindIndex(primeTable, index, Function(flag)
                                                       Return Not flag
                                                    End Function)
         If index >= 1 Then
            primes.Add(index)
            index += 1
         End If
      Loop
   End Sub

   Public Function GetAllPrimes() As Integer()
      Return primes.ToArray()
   End Function

   Public Function GetPrimesFrom(prime As Integer) As Integer()
      Dim start As Integer = primes.FindIndex(Function(value)
                                                 Return value = prime
                                              End Function)
      If start < 0 Then
         Throw New NotPrimeException(prime, String.Format("{0} is not a prime number.", prime))
      Else
         Return primes.FindAll(Function(value)
                                  Return value >= prime
                               End Function).ToArray()
      End If
   End Function
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:61,代码来源:Exception


示例9: Example

' 导入命名空间
Imports System.Reflection

Module Example
   Sub Main()
      Dim limit As Integer = 10000000
      Dim primes As New PrimeNumberGenerator(limit)
      Dim start As Integer = 1000001
      Try
         Dim values() As Integer = primes.GetPrimesFrom(start)
         Console.WriteLine("There are {0} prime numbers from {1} to {2}",
                           start, limit)
      Catch e As NotPrimeException
         Console.WriteLine("{0} is not prime", e.NonPrime)
         Console.WriteLine(e)
         Console.WriteLine("--------")
      End Try

      Dim domain As AppDomain = AppDomain.CreateDomain("Domain2")
      Dim gen As PrimeNumberGenerator = domain.CreateInstanceAndUnwrap(
                                        GetType(Example).Assembly.FullName,
                                        "PrimeNumberGenerator", True,
                                        BindingFlags.Default, Nothing,
                                        {1000000}, Nothing, Nothing)
      Try
         start = 100
         Console.WriteLine(gen.GetPrimesFrom(start))
      Catch e As NotPrimeException
         Console.WriteLine("{0} is not prime", e.NonPrime)
         Console.WriteLine(e)
         Console.WriteLine("--------")
      End Try
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:34,代码来源:Exception

输出:

1000001 is not prime
NotPrimeException: 1000001 is not a prime number.
at PrimeNumberGenerator.GetPrimesFrom(Int32 prime)
at Example.Main()
--------
100 is not prime
NotPrimeException: 100 is not a prime number.
at PrimeNumberGenerator.GetPrimesFrom(Int32 prime)
at Example.Main()
--------



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET Stream类代码示例发布时间:2022-05-24
下一篇:
VB.NET EventArgs类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap