forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.vb
121 lines (98 loc) · 2.72 KB
/
source.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'<snippet1>
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Public Class FileReaderException
Inherits Exception
Sub New(description As String)
MyBase.New(description)
End Sub
End Class
<Flags()> _
Public Enum ConnectionState
Closed = 0
Open = 1
End Enum
Public Class DemoDBClient
Private connstate As ConnectionState
Public Sub New()
connstate = ConnectionState.Open
End Sub
Public ReadOnly Property State As ConnectionState
Get
Return connstate
End Get
End Property
Public Sub Close()
connstate = ConnectionState.Closed
End Sub
End Class
Public Class FileUtils
Public Shared Function ReadFromFile(filename As String, bytes As Integer) As Byte()
Return File.ReadAllBytes(filename)
End Function
End Class
'<snippet4>
Public Class MyFileNotFoundException
Inherits Exception
End Class
'</snippet4>
Public Class ExceptionHandling
Public Shared Sub Main()
Dim conn As New DemoDBClient()
'<snippet2>
If conn.State <> ConnectionState.Closed Then
conn.Close()
End IF
'</snippet2>
'<snippet3>
Try
conn.Close()
Catch ex As InvalidOperationException
Console.WriteLine(ex.GetType().FullName)
Console.WriteLine(ex.Message)
End Try
'</snippet3>
End Sub
End Class
'<snippet5>
Class FileRead
Public Sub ReadAll(fileToRead As FileStream)
' This if statement is optional
' as it is very unlikely that
' the stream would ever be null.
If fileToRead Is Nothing Then
Throw New System.ArgumentNullException()
End If
Dim b As Integer
' Set the stream position to the beginning of the file.
fileToRead.Seek(0, SeekOrigin.Begin)
' Read each byte to the end of the file.
For i As Integer = 0 To fileToRead.Length - 1
b = fileToRead.ReadByte()
Console.Write(b.ToString())
' Or do something else with the byte.
Next i
End Sub
End Class
'</snippet5>
'<snippet6>
Class FileReader
Private fileName As String
Public Sub New(path As String)
fileName = path
End Sub
Public Function Read(bytes As Integer) As Byte()
Dim results() As Byte = FileUtils.ReadFromFile(fileName, bytes)
If results Is Nothing
Throw NewFileIOException()
End If
Return results
End Function
Function NewFileIOException() As FileReaderException
Dim description As String = "My NewFileIOException Description"
Return New FileReaderException(description)
End Function
End Class
'</snippet6>
'</snippet1>