VB.Net Tutorial/Development/Finally

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

Close Stream in Finally statement

Imports System.IO
public class Test
   public Shared Sub Main
        Dim srReader As IO.StreamReader
        Dim strLine As String
        Dim blnDone As Boolean = False
        Dim strFileName As String = "test.txt"
        "Read text from a file and display it in a list box.
        srReader = New IO.StreamReader(strFileName)
        Try
            While Not blnDone
                strLine = srReader.ReadLine()
                If strLine Is Nothing Then
                    blnDone = True
                Else
                    Console.WriteLine(strLine)
                End If
            End While
            srReader.Close()
        Catch ex As Exception
            Console.WriteLine("An error has occurred.")
        Finally
            srReader.Close()
        End Try

   End Sub
End class

Finally after multiple catch statements

Module Module1
    Sub Main()
        Dim intItem1 As Integer = 0
        Dim intItem2 As Integer = 128
        Dim intResult As Integer
        Try
            intResult = intItem2 / intItem1
        Catch e As OverflowException
            Console.WriteLine("An overflow exception occurred.")
        Catch e As OutOfMemoryException
            Console.WriteLine("Out of memory.")
        Catch e As IndexOutOfRangeException
            Console.WriteLine("Array index out of range.")
        Finally
            Console.WriteLine("Finally")
        End Try
    End Sub
End Module
An overflow exception occurred.
Finally

Finally always executes

Public Class Tester
    Public Shared Sub Main
         DoesNotThrowException()
    End Sub
   Public Shared Sub DoesNotThrowException()
      Try
         Console.WriteLine("In DoesNotThrowException")
      Catch
         Console.WriteLine("This Catch never executes")
      Finally
         Console.WriteLine("Finally executed in DoesNotThrowException")
      End Try
      Console.WriteLine("End of DoesNotThrowException")
   End Sub " DoesNotThrowException
End Class
In DoesNotThrowException
Finally executed in DoesNotThrowException
End of DoesNotThrowException

Try Catch with Finally clause

Option Strict On
 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 "DoDivide
     Shared Sub Main( )
         Try
             Console.WriteLine("Open file here")
             Dim a As Double = 5
             Dim b As Double = 0
             Console.WriteLine("{0} / {1} = {2}", a, b, DoDivide(a, b))
             Console.WriteLine("This line may or may not print")
         Catch e As System.DivideByZeroException
             Console.WriteLine("DivideByZeroException caught!")
         Catch
             Console.WriteLine("Unknown exception caught!")
         Finally
             Console.WriteLine("Close file here.")
         End Try
     End Sub
 End Class
Open file here
DivideByZeroException caught!
Close file here.