Visual Basic 2012 Voorbeelden
   

visual basic 2012 broncode voorbeelden

Blijf op de hoogte van de recente aanpassingen op vbvoorbeelden!

Microsoft Visual Studio 2012Microsoft Developers Network - Visual BasicMicrosoft .NET Framework

10.3. System.IO Excepties - IOExceptions

Print Email Deel op Twitter Deel op Facebook

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.

Net als zovele klassen en methods kunnen ook hier excepties optreden.

Voor de hand liggende excepties zijn deze waar je foutief zou verwijzen naar een bestand waarin wordt gelezen of geschreven, of excepties die optreden bij het benaderen van bestanden waar je niet voor ge-authoriseerd bent.
Visual Basic 2012 Broncode - Codevoorbeeld 302
Module IOExceptionsExample
    Sub Main()
        Dim path As String = "..."
        Dim streamReader As System.IO.StreamReader
        Try
            streamReader = New System.IO.StreamReader(path)
            Try
                streamReader.ReadToEnd() ' or : streamReader.ReadLine()
            Catch ex As System.OutOfMemoryException
                ' there is insufficient memory to allocate a buffer for
                ' the returned string
            Catch ex As System.IO.IOException
                ' an I/O error occurs
            End Try
        Catch ex As System.ArgumentNullException
            ' path is null
        Catch ex As System.ArgumentException
            ' path is an empty string
        Catch ex As System.IO.FileNotFoundException
            ' the file cannot be found
        Catch ex As System.IO.DirectoryNotFoundException
            ' the specified path is invalid, such as being on an unmapped drive
        Catch ex As System.IO.IOException
            ' path includes an incorrect or invalid syntax for file name,
            ' directory name, or volume label
        Finally
            If streamReader IsNot Nothing Then streamReader.Close()
        End Try
        '
        Dim streamWriter As System.IO.StreamWriter
        Try
            streamWriter = New System.IO.StreamWriter(path)
            Try
                streamWriter.Write("...")
            Catch ex As System.ObjectDisposedException
                ' AutoFlush is true or the buffer is full, and current writer
                ' is closed.
            Catch ex As System.NotSupportedException
                ' AutoFlush is true or buffer is full, and the contents of the
                ' buffer cannot be written to the underlying fixed size stream
                ' because the cursor is at the end the stream
            Catch ex As System.IO.IOException
                ' an I/O error occurs
            End Try
        Catch ex As System.UnauthorizedAccessException
            ' access is denied
        Catch ex As System.ArgumentNullException
            ' path is null
        Catch ex As System.ArgumentException
            ' path is an empty string ("") or path contains the name of a
            ' system device (com1, com2, and so on)
        Catch ex As System.IO.DirectoryNotFoundException
            ' the specified path is invalid, such as being on an unmapped drive
        Catch ex As System.IO.PathTooLongException
            ' the specified path, file name, or both exceed the system-defined
            ' maximum length
        Catch ex As System.IO.IOException
            ' path includes an incorrect or invalid syntax for file name,
            ' directory name, or volume label syntax
        Catch ex As System.Security.SecurityException
            ' the caller does not have the required permission
        Finally
            If streamWriter IsNot Nothing Then streamWriter.Close()
        End Try
        '
        Console.ReadLine()
    End Sub
End Module

Dit artikel is gepubliceerd op maandag 15 oktober 2012 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.