我正在使用一个TRY Catch Block而且我得到的错误并期望在这里落地:
Throw New System.Exception("Copy of FileZilla file failed: " & ex.Message)
我得到的输出按以下顺序:
创建FileZilla文件夹失败: 创建FileZilla文件失败: FileZilla文件的副本失败:拒绝访问路径“C:\ Program Files(x86)\ FileZilla Server”。
为什么它会击中另外2并显示它们?
Dim sourceFile As FileInfo = New FileInfo(strSourcePathAndFile)
Try
' Create the folder if it does not exist.
If Not Directory.Exists(strDestinationFolder) Then
Directory.CreateDirectory(strDestinationFolder)
End If
Try
' Create the file if it does not exist.
If Not File.Exists(strDestinationPathAndFile) Then
File.Create(strDestinationPathAndFile)
End If
Try
' Copy the source file to the destination file and overwrite it.
sourceFile.CopyTo(strDestinationPathAndFile, True)
Catch ioex As IOException
Throw New System.Exception("Copy of FileZilla file I/O failed: " & ioex.Message)
Catch ex As Exception
Throw New System.Exception("Copy of FileZilla file failed: " & ex.Message)
End Try
Catch ex As Exception
Throw New System.Exception("Create of FileZilla file failed: " & ex.Message)
End Try
Catch ex As Exception
Throw New System.Exception("Create of FileZilla folder failed: " & ex.Message)
End Try
End Sub
...问候
答案 0 :(得分:2)
因为你抛出新的异常,它被另一个Catch块捕获,冲洗和放大重复。
你的外部处理程序处理通用的异常类型,也许这就是你要求的。将其更改为处理其他类型的异常。或者只需要一个Try ... Catch block。
答案 1 :(得分:1)
你正在捕捉三个抛出异常,因为你有三个级别的Try ... Catch。
你还有什么期待?
最内部的Catch引发了一个新的异常。这自然被中间Catch捕获,然后抛出一个新的异常,它被最外层的捕获所捕获。
答案 2 :(得分:0)
那是因为您正在使用嵌套的try..catch
块。错误在几个级别为catch
。您可以尝试以下方法:
Dim sourceFile As FileInfo = New FileInfo(strSourcePathAndFile)
Dim error as String
Try
' Create the folder if it does not exist.
If Not Directory.Exists(strDestinationFolder) Then
Directory.CreateDirectory(strDestinationFolder)
End If
Try
' Create the file if it does not exist.
If Not File.Exists(strDestinationPathAndFile) Then
File.Create(strDestinationPathAndFile)
End If
Try
' Copy the source file to the destination file and overwrite it.
sourceFile.CopyTo(strDestinationPathAndFile, True)
Catch ioex As IOException
If String.IsNullOrEmpty(error) then _
error = string.concat("Copy of FileZilla file I/O failed: ", ioex.Message)
Catch ex As Exception
If String.IsNullOrEmpty(error) then _
error = string.concat("Copy of FileZilla file failed: ", ex.Message)
End Try
Catch ex As Exception
If String.IsNullOrEmpty(error) then _
error = string.concat("Create of FileZilla file failed: ", ex.Message)
End Try
Catch ex As Exception
Throw New System.Exception(string.concat(error, ex.Message)
End Try
End Sub