Catch Block Code

I have a VB.Net application which is slowly being converted to use classes rather than subs on every form.

Each form has its own, sometimes many Try, Catch, Finally blocks.

In the main the Catch blocks are the same, as are the Finally blocks.

Can I move the Catch Blocks code to an "error handling class"?

If so, how do I handle the Catch declaration? Typically  Catch sqlex As SqlClient.SqlException

Thanks
Paul BrooksAsked:
Who is Participating?
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.

käµfm³d 👽Commented:
A Catch block has to be attached to a Try block--you cannot detach it. You could, though, create a class that you use to log details of the exception, or alert someone that there was a problem.
0
Paul BrooksAuthor Commented:
Thanks for responding.

I wasn't thinking about detaching it but I did have in mind to create a class to log the exceptions.

When I tried it I had an error with "sqlex" being undeclared in my class because it is declared in the Catch statement thus ....


Catch sqlex As SqlClient.SqlException

How do I declare in the Catch statement then use it in my error class?
0
käµfm³d 👽Commented:
I'm not sure what state you would want to maintain within a logging class, so I would surmise that such a class would be Shared (a.k.a. static). If you created such a class, then you would simply need to create a method that has the appropriate signature.

e.g.

Public Shared Class Logger
    Public Shared Sub LogException(ByVal sqlEx As SqlClient.SqlException)
        // log the exception
    End Sub
End Class

Open in new window


And then you would simply refer to that in your Catch block:

e.g.

Try
    // do something
Catch ex As SqlClient.SqlException
    Logger.LogException(ex)
End Try

Open in new window


Since the LogException method above is defined as Shared, you use the class name (Logger) instead of an instance variable when you call the method.
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
Paul BrooksAuthor Commented:
Thanks - that looks like the kind of thing I think I need.

I'll give it  ago now.
0
Paul BrooksAuthor Commented:
Just what I needed, perfect.
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
.NET Programming

From novice to tech pro — start learning today.