Reputation: 77
I've been writing a class library containing business logic which will be used within a new winforms project and an existing asp.net project.
During development I littered the library with
Try
'Whatever
Catch ex as exception
Msgbox("Friendly error message", ex.message)
end try
Which will work fine for winforms, but won't work for asp.net I need a friendly (and easy) way of trapping exceptions that will work for both platforms. Or a better question could probably be SHOULD I be trapping exceptions in the Class Library at all?
Upvotes: 3
Views: 215
Reputation: 27322
If only you will be consuming the library then you can decide whether you want to log the exception or not. If your library will be used by a third party then I would suggest not logging the exception.
But in either case you should throw the exception so that the calling app can then decide what to do with it.
I have created assemblies that are used by both VB6 and VB.NET applications and I log the exceptions thrown in the assembly because the VB6 apps cannot access the stack trace to log it
Upvotes: 2
Reputation: 101150
Or a better question could probably be SHOULD I be trapping exceptions in the Class Library at all?
No. Class libraries should never do something with exceptions. You just obfuscate the real reason to why an error occurred, making it a lot harder for the developer to understand what went wrong.
It doesn't matter if that developer is you or another one. Because when you return to the code one year from now you will not have a clue what really happened (unless the original exception is intact).
As for exception handling I am 100% opposed having try/catch statements everywhere in the application. There is really no benefit. In WinForms use the Application.ThreadException
event to display a message box to the user. In ASP.NET override the Error
event to log the exceptions and display a friendly error page.
I've also created an service called OneTrueError which takes care of everything for you. It got a free edition that you can use.
Upvotes: 2
Reputation: 496
I prefer not logging in the library and would better throw the exception for the consumer to handle it.
Upvotes: 2
Reputation: 2522
You should really rather be logging the messages centrally. We use a web service. Look at some of the free error handling/trapping apps inside visual studio addons. Some are free to a point (See Gibraltar Loupe - in extensions).
If you really must, create a separate class, that you can all and let it determine the transport and logging mechanism.
Upvotes: 1