Chris
Chris

Reputation:

Is there a better way to get visual studio to ignore try/catch in debug mode

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?

Upvotes: 6

Views: 5342

Answers (4)

Magnus
Magnus

Reputation: 1754

Here is how I do it:

Enabled:

Try ' : Catch: End Try
  doSomething
  andSomeMore
  etcEtc
' Try
Catch ex As Exception
  handleException
End Try

Disable the above by deleting the two comment characters:

Try : Catch : End Try
  doSomething
  andSomeMore
  etcEtc
Try
Catch ex As Exception
  handleException
End Try

Upvotes: 0

Martin Peck
Martin Peck

Reputation: 11544

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)

Upvotes: 16

JaredPar
JaredPar

Reputation: 754555

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

Upvotes: 3

Nate
Nate

Reputation: 30636

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.

Upvotes: 0

Related Questions