Error Handling in Function vb.net

Hi All ... learning VB.NET as a hobby ...

How can I properly implement an error handling routine in the following function and return true for the process being started and false for an error ...
   ' pass in a fully qualified filename
    Public Function fnShellOpenVB(fn As String)
        Dim p As New System.Diagnostics.Process
        Dim s As New System.Diagnostics.ProcessStartInfo(fn)
        s.UseShellExecute = True
        p.StartInfo = s
        p.Start()
    End Function

Open in new window


MTIA

DWE
LVL 1
dwe0608Asked:
Who is Participating?
 
Shaun VermaakConnect With a Mentor Technical Specialist/DeveloperCommented:
I would rather do it this way...

I would not show message via the function. Also this way you can use as true/false and look at message if you want to

    Public Function fnShellOpenVB(fn As String, ByRef message As String) As Boolean
        Try
            Dim p As New System.Diagnostics.Process
            Dim s As New System.Diagnostics.ProcessStartInfo(fn)
            s.UseShellExecute = True

            p.StartInfo = s
            p.Start()
            message = "Success"
            fnShellOpenVB = True
        Catch e As Exception
            message = e.Message + " (" + e.ToString() + ")"
            fnShellOpenVB = False
        End Try
    End Function

Open in new window

0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
   ' pass in a fully qualified filename
    Public Function fnShellOpenVB(fn As String) As Boolean
        Try
            Dim p As New System.Diagnostics.Process
            Dim s As New System.Diagnostics.ProcessStartInfo(fn)
            s.UseShellExecute = True
            p.StartInfo = s
            p.Start()
            Return true
        Catch
            Return false
        End Try
    End Function

Open in new window

0
 
dwe0608Author Commented:
Hi Shaun,

Thanks for the fast response. So the user has some feedback on the error, would the following work?

    Public Function fnShellOpenVB(fn As String) As Boolean
        Try
            Dim p As New System.Diagnostics.Process
            Dim s As New System.Diagnostics.ProcessStartInfo(fn)
            s.UseShellExecute = True

            p.StartInfo = s
            p.Start()
            fnShellOpenVB = True
        Catch e As Exception
            MsgBox(e.Message + " (" + e.ToString() + ")")
            fnShellOpenVB = False
        End Try

    End Function

Open in new window


MTIA
DWE
0
 
dwe0608Author Commented:
Thanks Shaun ... Great Answer ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.