exceptions/inner exceptions

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

hi all,
using the following code, i never get a message box showing the error, but the default
error sound is produced (like there should be an accompanying messagebox).

\\\\\\\\\\
Dim FI As FileInfo = _
My.Computer.FileSystem.GetFileInfo(txtSelectedDocument.Text)

Try
My.Computer.FileSystem.CopyDirectory(FI.DirectoryName, _
lblResultText.Text, _
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, _
FileIO.UICancelOption.ThrowException)
MsgBox("Update is complete.", MsgBoxStyle.Information, _
"Complete.")
Catch ex As Exception
MsgBox("Copying did not finish." & DoubleCrLf & "Error: " _
& ex.InnerException.ToString, MsgBoxStyle.Critical, _
"Failed.")
End Try
\\\\\\\\\\\

how can i get the message box to show up, and to show up with the error?
thanks,
lance
 
i changed the line

Catch ex As Exception

to

Catch ex As OperationCanceledException


but now when i press the cancel button on the copy directory dialog box, all i get is the
exception helper saying that the TargetInvocationException was unhandled.

i put a breakpoint at the "Catch....:" line to check it out. the excpetion helper appears
after trying to execute the msgbox line.
 
i'd like to be able to properly catch the cancellation of the directory copy, but still no
luck displaying the messagebox (i'm still just only getting the default error chime). any
other suggestions would be greatly appreciated.

when stepping through the code, it gets to the MsgBox line, and the never actually shows
it.

lance
 
if i change the messagebox to debug.print then i get the appropriate message in the
immediate window:

"A first chance exception of type 'System.OperationCanceledException' occurred in
Microsoft.VisualBasic.dll
The operation was canceled."

but using a messagebox is causing problems (no box, just sound)

lance
 
Lance said:
using the following code, i never get a message box showing the error
Catch ex As Exception
MsgBox("Copying did not finish." & DoubleCrLf & "Error: " _
& ex.InnerException.ToString, MsgBoxStyle.Critical, _
"Failed.")

Since you're catching Exception, you can't guarantee that the
InnerException property is going to have a value. If it arrives as a
null reference, the code that calls "ex.InnerException.ToString" will fail.

Only catch the type of Exception you want, or make sure that you've got
a valid Inner Exception before displaying it, as in

Catch ex As Exception
If ex.InnerException Is Nothing Then
MsgBox("Copying did not finish." & DoubleCrLf _
& "Error: " & ex.ToString() _
, MsgBoxStyle.Critical _
, "Failed." _
)
Else
MsgBox("Copying did not finish." & DoubleCrLf _
& "Error: " & ex.InnerException.ToString() _
, MsgBoxStyle.Critical _
, "Failed." _
)
End If

HTH,
Phill W.
 
Back
Top