VB.Net Tutorial/Development/Try Catch

Материал из VB Эксперт
Перейти к: навигация, поиск

Catch custom exception

<source lang="vbnet">Public Class MyException

   Inherits System.Exception
   Sub New(ByVal Message As String)
       MyBase.New(Message)
   End Sub

End Class

Public Class Tester

   Public Shared Sub Main
       Try
           Console.WriteLine("About to generate custom exception")
           Throw (New MyException("** Custom Message **"))
       Catch Ex As MyException
           Console.WriteLine("Custom Exception thrown " & Ex.Message)
       End Try
   End Sub

End Class</source>

About to generate custom exception
Custom Exception thrown ** Custom Message **

Catch different Exception with multiple catch statements

<source lang="vbnet">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</source>

You have encountered an overflow exception.
1/2 = 0

Catch Exception throwed from a Method

<source lang="vbnet">Option Strict On

Imports System
Class Tester
    Shared Sub Main( )
        Console.WriteLine("Enter func1...")
        Try
            Console.WriteLine("Entering Try block...")
            Func1( )
            Console.WriteLine("Exiting Try block...")
        Catch
            Console.WriteLine("Exception caught and handled")
        End Try
        Console.WriteLine("Exit func1...")
    End Sub
    Public Shared Sub Func1( )
        Console.WriteLine("Enter Func2...")
        Throw New System.Exception( )
        Console.WriteLine("Exit Func2...")
    End Sub
End Class</source>
Enter func1...
Entering Try block...
Enter Func2...
Exception caught and handled
Exit func1...

Catch specific Exception from a Method

<source lang="vbnet">Imports System

Class Tester
    Public Shared Function DoDivide(ByVal a As Double, ByVal b As Double) As Double
        If b = 0 Then
            Throw New System.DivideByZeroException( )
        End If
        If a = 0 Then
            Throw New System.ArithmeticException( )
        End If
        Return a / b
    End Function
    Public Shared Sub Main( )
        Try
            Dim a As Double = 5
            Dim b As Double = 0
            Console.WriteLine("Dividing {0} by {1}...", a, b)
            Console.WriteLine("{0} / {1} = {2}",a, b, DoDivide(a, b))
        Catch e As System.DivideByZeroException
            Console.WriteLine("DivideByZeroException caught!")
        Catch e As System.ArithmeticException
            Console.WriteLine("ArithmeticException caught!")
        Catch
            Console.WriteLine("Unknown exception caught")
        End Try
    End Sub
End Class</source>
Dividing 5 by 0...
DivideByZeroException caught!

Catch without Exception type

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim strCaption As String
       strCaption = "Answer"
       Try
           Console.WriteLine(strCaption.Substring(10, 1))
       Catch
           Console.WriteLine("An error has occurred.")
       End Try
  End Sub

End class</source>

An error has occurred.

Filtered Exception

<source lang="vbnet">Option Strict Off Public Module Tester

  Public Sub Main
     Dim a, b As Integer
     Try
        
        a = 0
        b = 0
        a = a/b
     Catch e As DivideByZeroException When a = 0
        Console.WriteLine("Division by zero error: " & _
                         "the first divisor must not be zero.")
     Catch e As DivideByZeroException When b = 0
        Console.WriteLine("Division by zero error: " & _
                          "the second divisor must not be zero.")
     Catch e As Exception
        Console.WriteLine("Error: " & e.Message)
     End Try
  End Sub

End Module</source>

Error: Arithmetic operation resulted in an overflow.

Try Catch Exception

<source lang="vbnet">Option Strict On

Imports System
Class Tester
    Shared Sub Main( )
        Console.WriteLine("Enter Func2...")
        Try
            Console.WriteLine("Entering Try block...")
            Throw New System.Exception( )
            Console.WriteLine("Exitintg Try block...")
        Catch
            Console.WriteLine("Exception caught and handled")
        End Try
       Console.WriteLine("Exit func2...")
    End Sub "Main
End Class "Tester</source>
Enter Func2...
Entering Try block...
Exception caught and handled
Exit func2...