Visual studio 是否使用Try/Catch块设置文件路径?
我在计算Visual Basic课程的作业时遇到了一些困难。我被告知假定给定的文本文件不在程序的\bin\Debug文件夹中,因此我试图抛出一个异常错误,并通过inputbox从用户那里获取正确的路径,但似乎什么也没有发生,或者变量没有设置,我不完全确定是哪个。我在下面有我的代码,关于为什么这对我不起作用有什么提示吗 谢谢Visual studio 是否使用Try/Catch块设置文件路径?,visual-studio,visual-studio-2013,Visual Studio,Visual Studio 2013,我在计算Visual Basic课程的作业时遇到了一些困难。我被告知假定给定的文本文件不在程序的\bin\Debug文件夹中,因此我试图抛出一个异常错误,并通过inputbox从用户那里获取正确的路径,但似乎什么也没有发生,或者变量没有设置,我不完全确定是哪个。我在下面有我的代码,关于为什么这对我不起作用有什么提示吗 谢谢 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.L
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader
Dim age As Integer
Dim path As String
Try
sr = IO.File.OpenText("Ages.txt")
age = CInt(sr.ReadLine)
txtOutput.Text = "Age is " & age
Catch exc As IO.FileNotFoundException
path = InputBox("File Ages.txt not found." & vbCrLf & "Please enter the correct path to the file.", _
"Example: C:\Documents\My Text Files")
Catch exc As InvalidCastException
MessageBox.Show("File 'Ages.txt' contains an invalid age.", "Warning!")
Try
sr = IO.File.OpenText(path)
Finally
txtOutput.Text = "Age is " & age
End Try
Finally
Try
sr.Close()
Catch
End Try
End Try
End Sub
内部Try/Catch块应位于FileNotFoundException捕获内
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader
Dim age As Integer
Dim path As String
Try
sr = IO.File.OpenText("Ages.txt")
age = CInt(sr.ReadLine)
txtOutput.Text = "Age is " & age
Catch exc As IO.FileNotFoundException
path = InputBox("File Ages.txt not found." & vbCrLf & "Please enter the correct path to the file.", _
"Example: C:\Documents\My Text Files")
Try
sr = IO.File.OpenText(path)
Finally
txtOutput.Text = "Age is " & age
End Try
Catch exc As InvalidCastException
MessageBox.Show("File 'Ages.txt' contains an invalid age.", "Warning!")
Finally
Try
sr.Close()
Catch
End Try
End Try
End Sub
更好的解决办法是
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim age As Integer
Dim path As String = "Ages.txt"
Dim fileFound As Boolean = False
While Not IO.File.Exists(path)
path = InputBox("File Ages.txt not found." & vbCrLf & "Please enter the correct path to the file.", _
"Example: C:\Documents\My Text Files")
End While
Using sr As IO.StreamReader = IO.File.OpenText(path)
If Integer.TryParse(sr.ReadLine, age) Then
txtOutput.Text = "Age is " & age
Else
MessageBox.Show("File 'Ages.txt' contains an invalid age.", "Warning!")
End If
End Using
End Sub
这将首先检查文件是否存在,并继续循环,直到用户输入存在的文件。这允许您删除FileNotFoundException捕获
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader
Dim age As Integer
Dim path As String
Try
sr = IO.File.OpenText("Ages.txt")
age = CInt(sr.ReadLine)
txtOutput.Text = "Age is " & age
Catch exc As IO.FileNotFoundException
path = InputBox("File Ages.txt not found." & vbCrLf & "Please enter the correct path to the file.", _
"Example: C:\Documents\My Text Files")
Try
sr = IO.File.OpenText(path)
Finally
txtOutput.Text = "Age is " & age
End Try
Catch exc As InvalidCastException
MessageBox.Show("File 'Ages.txt' contains an invalid age.", "Warning!")
Finally
Try
sr.Close()
Catch
End Try
End Try
End Sub
这还使用Integer.TryParse检查值是否正确强制转换,从而允许您删除InvalidCastException捕获
最后,使用Using语句打开读卡器消除了对finally/nestedtry-catch块的需要,因为Using在内部实现了这一点 我敢打赌抛出的异常不是一个
IO.FileNotFoundException
,因此您的输入框
代码永远不会被命中。可以引发很多类型的异常。有几种方法可以毫无例外地执行此操作(我想到),但要继续此过程,我会将您的异常处理更改为以下内容:
Catch exc as Exception
If TypeOf exc Is InvalidCastException Then
'...
Else
path = InputBox(....)
End If
End Try
当你说什么都没有发生时,你是说输入框没有被显示吗?