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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Shaun VermaakTechnical 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dwe0608Author Commented:
Thanks Shaun ... Great Answer ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.