本文整理汇总了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
输出:
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
示例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
输出:
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
示例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
输出:
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
输出:
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
示例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
示例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
输出:
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() --------