views:

608

answers:

4

I want the designer to catch the error when I am debugging and I want the user to see my friendly message if an error occurs for them. I know I can acomplish this with the following:

#If Debug=False Then

Try

#End If

'some code here

#If Debug=False Then

Catch ex as exception

    Messagebox.Show("Errors suck")

End Try

#End If

I do not want to have to write all the #statements and having them cluttering up my code. It seems like this should be a common need and there has to be a better way. Does anyone know a better way?

A: 

In the catch section of your Try..Catch you should write the exception message, stacktrace, and anything else you may want to a log file -- additionally you could write that data to the Windows Event log as well.

At worst, you could just put break-points in the Catch section of your Try..Catch blocks, since you shouldn't normally hit them it should'nt be a big deal once setup.

Nate Bross
+11  A: 

In VS.NET you can say whether you want the debugger to break when an exception is thrown (not just when it's unhandled).

Look on the Debug | Exceptions... menu item (Ctl-Alt-E on my keyboard shortcuts). Pick the excepption you're interested in, then tick the "thrown" box for that exception.

You can tick that box at several levels (all CLR exceptions, all CLR exceptions in a given namespace, or very specific exceptions)

Martin Peck
+2  A: 

There is no good way to get it to ignore a try catch other than what you've done. But you can make the code a little bit cleaner and essentially get the same effect. You essentially are trying to prevent the action in the catch block from running. A better way to do that is a conditionally compiled method.

Try
... 
Catch ex As Exception
  DebugLog(ex)
  Throw
End Try

<Condition("DEBUG)> _
Public Sub DebugLog(ByVal ex As Exception) 
  Messagebox.Show("errors suck")
End Sub
JaredPar
A: 

I notice you never marked anything as accepted here. Have you heard of Aspect Oriented Programming? This is actually similar to something I am looking to implement in a new project, so I was curious if you had done anything else with this. Plus it being a year old question ... you may have done something and failed to followup.

And then it dawns on me that it looks like the asker has closed his account ...

drachenstern