Basically what I am doing is executing some code (which is in a try catch block) and if all goes well I call a function which is in a try catch. If there is an exception in the function the code hits the catch block but still returns to the calling code and another exception is hit there even though that code runs fine.
I hope this makes sense. Here is some sample code
Private Sub ConnectToFTP()Try
Client.Hostname = "asdf.adf.net"
Client.Username = "asdf"
Client.Password = "adsf"
'NOTE: We are connecting in acitve mode
Client.Passive = False
Client.Connect()If Not Client.IsConnected Then
mobjError.ErrorMessage = "Service unavailable: Ensure the computer is connected to the Internet. Please exit and try again!"
WinFun.InsertErrorInfo(intCustomerId, "", "", "Service unavailable: Ensure the computer is connected to the Internet. Please exit and try again!")
Client = Nothing
Throw New ApplicationException()
End IfCatch exc As Exception
mobjError.ErrorMessage = "Service unavailable: Ensure the computer is connected to the Internet. Please exit and try again!"
WinFun.InsertErrorInfo(intCustomerId, "", "", "Service unavailable: Ensure the computer is connected to the Internet. Please exit and try again!")
WinFun.InsertErrorInfo(intCustomerId, exc.Source, exc.StackTrace, exc.Message)
'Clear All Objects
Client = Nothing
WinFun.Dispose()
WinFun = Nothing
End Try
ValidateSerial()
End Sub
Public Sub ValidateSerial()
Dim objWMIService
Dim objDrive As Object
Dim colDrive
TryDim strComputer As String = "."
objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
colDrive = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
For Each objDrive In colDriveDim strSerial As String
strSerial = objDrive.PnpDeviceID.ToString'Get everything after the last whack (\)
If strSerial.IndexOf("\") > 0 Then
strSerial = strSerial.Remove(0, strSerial.LastIndexOf("\") + 1)
End If'Remove everything after the last ampersand including the last ampersand (&)
If strSerial.IndexOf("&") > 0 Then
strSerial = strSerial.Remove(strSerial.LastIndexOf("&"), strSerial.Length - strSerial.LastIndexOf("&"))
End IfDim intCheckSerial As Integer
intCheckSerial = WinFun.CheckCustomer(strSerial)If intCheckSerial <> -1 Then
intCustomerId = intCheckSerial
Exit For
End IfNext
If intCustomerId Is Nothing Then
Throw New ApplicationException()
End IfCatch exc As Exception
mobjError.ErrorMessage = "If the problem persists contact support personal."
WinFun.InsertErrorInfo(intCustomerId, exc.Source, exc.StackTrace, exc.Message)
'Clear All Objects
Client = Nothing
Throw
Finally
'objWMIService.dispose()
'objDrive.dispose()
'colDrive.dispose()
'objWMIService = Nothing
'objDrive = Nothing
'colDrive = Nothing
End Try
End Sub
So if there is an error in the second functio it goes back to the first funciton as well and hits its catch block too.if you mean what I think you mean, your CATCH block should probably do an Exit Sub
or possibly you should have a catch block in the calling function - I don't use VB.NET so I'm not 100% sure which would be required here.
0 comments:
Post a Comment