【发布时间】:2020-02-28 12:53:12
【问题描述】:
我在一行代码周围有一个 try-catch,它可以生成异常,但是,catch 没有捕获它。我仍然得到一个未处理的异常。这是为什么呢?
在此示例中,我已经有一个位于 C:\temp\defect.bmp 的图像,当我尝试使用相同的路径保存图像时,我在昏暗的 imageStream 代码行上收到 System.IO.IOException,因为我错误地使用了 CreateNew 模式,所以这应该会抛出异常,但是为什么 Catch ex as Exception 没有捕获它呢?
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim image As New WpfImage("C:\TEMP\defect.bmp")
Try
Dim imageStream As New IO.FileStream("C:\TEMP\defect.bmp", IO.FileMode.CreateNew)
image.Save(imageStream, ImageFormat.BMP)
Catch ex As Exception
MsgBox("This exception has been caught: " & ex.Message)
End Try
End Sub
End Class
【问题讨论】:
-
只是为了确定:异常不是来自
New WpfImage("C:\TEMP\defect.bmp")吗? -
WpfImage 是什么?它不是框架的一部分。
标签: wpf vb.net exception try-catch