我最近寫了一些與菜單條相關的代碼,有一個選項包含OpenFile,SaveFile和Exit等。以下是代碼。SaveFileDialog.OpenFile()IndexOutofRangeException
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "ebd files (*.txt)|*.txt"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
Dim sw As New StreamWriter(myStream)
sw.Flush()
sw.Close()
myStream.Close()
End If
Catch Ex As Exception
MessageBox.Show("Can't save file on disk: " & Ex.Message)
End Try
End If
另一個代碼塊完全相同,但withou如果聲明,如下,
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "ebd files (*.txt)|*.txt"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
Try
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
Dim sw As New StreamWriter(myStream)
'some sw.WriteLine code here...
sw.Flush()
sw.Close()
myStream.Close()
End If
Catch Ex As Exception
MessageBox.Show("Can't save file on disk: " & Ex.Message)
End Try
這個問題我已經是第二一段代碼,在運行時,系統會拋出指數出來的範圍異常,我如何解決它而不使用If語句?有沒有任何方法可以顯示對話窗口?任何人都可以給我這個索引錯誤信息的線索?謝謝!
高賠率的異常代碼中,我們看不到異常。您需要發佈異常的堆棧跟蹤。 –