VB.Net Tutorial/Development/Predefined Exception

Материал из VB Эксперт
Версия от 12:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

An overflow exception occurred

Module Module1
    Sub Main()
        Dim intItem1 As Integer = 0
        Dim intItem2 As Integer = 128
        Dim intResult As Integer
        Try
            intResult = intItem2 / intItem1
            Console.WriteLine("The answer is ", intResult)
            Console.WriteLine("Press Enter to continue...")
            Console.ReadLine()
        Catch
            Console.WriteLine("An overflow exception occurred.")
        End Try
    End Sub
End Module
An overflow exception occurred.

Catch ApplicationException and ArgumentException

Option Strict On
Public Module Tester
   Public Sub Main()
      Try
         RoutineA()
      Catch e As Exception
         Console.WriteLIne("Main error handler: " & e.Message)
      End Try
   End Sub
   Private Sub RoutineA()
      Try
         Throw New ApplicationException
      Catch e As ApplicationException
         Console.WriteLine("Exception in RoutineA: " & e.Message)
      End Try
      Try
         Throw New ArgumentException
      Catch e As ApplicationException
         Console.WriteLine("Exception in RoutineA: " & e.Message)
      End Try
      Console.WriteLine("About to exit RoutineA.")
   End Sub
End Module
Exception in RoutineA: Error in the application.
Main error handler: Value does not fall within the expected range.

Catch EndOfStreamException

Imports System.IO
public class Tester
   public Shared Sub Main
        Dim Stream As IO.StreamReader = IO.File.OpenText("test.txt")
        Try
            Console.WriteLine(Stream.ReadLine())
        Catch e As EndOfStreamException
            Console.WriteLine("EndOfStreamException")
        Catch e As IOException
            Console.WriteLine(e.Message)
        Finally
            Stream.Close()
        End Try
   End Sub
   
End class

Catch FormatException

Public Class Tester
    Public Shared Sub Main
    
      Try
         Dim numerator As Integer = Convert.ToInt32("123")
         Dim denominator As Integer = Convert.ToInt32("321")
         Dim result As Integer = numerator \ denominator
         Console.WriteLine(result)
      Catch formattingException As FormatException
         Console.WriteLine("You must enter two integers")
      Catch dividingException As DivideByZeroException
         Console.WriteLine(dividingException.Message)
      End Try
    End Sub
End Class
0

InvalidCastException

Option Strict On
Public Module ThrowStatement
   Public Function GetEvenNumber() As Integer
      Dim inputString As String = "asdf"
      Throw New InvalidCastException("Unable to convert "" & inputString & "" to an integer")
      Return 0
   End Function

   Public Sub Main
         Try
            Console.WriteLine(GetEvenNumber())
         Catch e As InvalidCastException
            Console.WriteLine(e.GetType().Name & ": " & e.Message)
         Catch e As Exception
            Console.WriteLine(e.GetType().Name & ": " & e.Message)
         End Try
   End Sub
End Module
InvalidCastException: Unable to convert "asdf" to an integer

System.Data.ConstraintException

Imports System.IO
public class Tester
   public Shared Sub Main
        Dim y As Integer
        Dim x As Integer
        Dim z As Integer
        Try
            x = 1
            y = 0
            z = x / y
        Catch e As DivideByZeroException
            Console.WriteLine("You have attempted to divide by zero!")
            y = 2
            z = x / y
        Catch e As OverflowException
            Console.WriteLine("You have encountered an overflow exception. ")
            y = 2
            z = x / y
        Catch e As System.Data.ConstraintException
            Console.WriteLine("You have encountered a constraint exception. ")
            y = 2
            z = x / y
        Finally
            Console.WriteLine(x & "/" & y & " = " & z)
        End Try
   End Sub
   
End class
You have encountered an overflow exception.
1/2 = 0

Throw ArgumentOutOfRangeException

Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Tester
    Public Shared Sub Main
        Dim number As Decimal
        number = CDec(Val("123.123"))
        Console.WriteLine("Factorial(" & number & ")... ")
        Console.WriteLine(Factorial(number))
        
    End Sub
    Public Shared Function Factorial(ByVal number As Decimal) As Decimal
        Select Case number
            Case Is < 0
                Throw New ArgumentOutOfRangeException( _
                    "Negative numbers not allowed.")
            Case Is = 0
                Return 1
            Case Else
                Return number * Factorial(number - 1)
        End Select
    End Function

End Class
Factorial(123.123)...
Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of
valid values.
Parameter name: Negative numbers not allowed.
   at Tester.Factorial(Decimal number)

Throw ArithmeticException

Module Module1
    Sub Main()
            Try
                AddNumbers(1, 2)
            Catch Except As Exception
                Console.WriteLine("Call generated error: " & Except.Message)
            End Try
    End Sub
    Function AddNumbers(ByVal A As Integer, ByVal B As Integer) As Integer
        Try
            Throw New ArithmeticException()
        Catch Except As Exception
            Console.WriteLine(Except.Message)
        End Try
    End Function
End Module
Overflow or underflow in the arithmetic operation.

Throw DivideByZeroException

Module Module1
    Sub Main()
            Try
                DoMod(1, 2)
            Catch Except As Exception
                Console.WriteLine("Call generated error: " & Except.Message)
            End Try
    End Sub
    Function DoMod(ByVal A As Integer, ByVal B As Integer) As Integer
        Try
            Throw New DivideByZeroException()
        Catch Except As Exception
            Console.WriteLine(Except.Message)
        End Try
    End Function
End Module
Attempted to divide by zero.