views:

62

answers:

1

I recently migrated a VB6 app to VB.Net. Entire VB6 dependency are removed . And the code was working fine just for a single module mean to say like for WinApp it is working fine.

Now my requirement has been changed, now the same class will be accessed by multiple application , it might a Windows App, Web App or a web service. So I am not able decide any efficient error handling pattern.

So you you guys help me out. Currently what I am doing is that parent function , i am passing two vars LogError as bool and ErrorMessage as string parameter, that will further check something like this

Catch(ex as Exception)
     If LogError then
          MessageBox.Show("My_Module_Name :" & ex.Message)
     EndIf
     ErrorMessage = ex.Message
End Try

Also same catch block is used in all other functions/ subroutines. So what I need is any elegant handling method that will work efficiently on cross-app platform(win/web/service)

+5  A: 

I'd suggest logging the messages, either to a log file or to the Event log, but why not let the clients choose, in that you could add some methods to let the client decide where it should be logged. Though instead of having your exception handler handle the message and put it into an errormessage variable I'd just follow the logging with a throw so that the Exception continued up in the call chain until some code that knew how to handle it got it.

In general it's not good practice to catch Exception, you should catch only the exceptions that you can handle. But I think it's ok if you just want to log it and then will re-throw it again.

ho1
As well as writing to a file or to the error log it is also worth noting that the exception class is serializable and can be serialised and stored to a database
zeocrash
Ho , If i am right then you want to saySay ABC is a core class of my application, accessed by various multiple clients.Public Class ABC Public sub XYZ() Try .statements..... Catch(ex as Exception) Throw ex End Try End FunctionEnd ClassClient 1 (WinForm)Protected void btnSave_Click(Object sender, EventArgs e ){ ABC abc = new ABC(); try { abc.XYZ(); } catch(Exception ex) { // logging or displaying }}i hope, i am far clear
Amit Ranjan
If you're not going to do anything in the XYZ exception handler, just remove it since it's just a waste of space, I'd just keep it if I were actually going to do the logging in there. You should try to avoid doing catch(Exception) in btnSave and instead handle the exceptions that you can handle (for example, if it failed due to no access, maybe you can just tell the user to choose different directory to save or similar).
ho1