Solved

Conditional TRY/CATCH

Posted on 2006-11-05
5
469 Views
Last Modified: 2011-09-20

Is there a way I can make my try/catches conditional?  What I want to do is turn them all off globally, because in the development environment it is easier to let the debugger catch the exceptions (because then I can watch vars, etc), rather than my catch code process it.  But I don't want to comment out a million try/catches to do this.  Ideally I'd like something like:

Const TRY_CATCH_ENABLED as Boolean = True

If TRY_CATCH_ENABLED Then Try

(code)

If TRY_CATCH_ENABLED Then Catch ex as Exception

(handling)

If TRY_CATCH_ENABLED Then End Try

Then I could turn off all my exception handling globally.  Obviously the above isn't even close to correct, but it gives you the idea of what I want to do.  Any suggestions?

(I know this is possible with On Error statements, but hopefully I don't  have to revert back to those.)
0
Comment
Question by:riceman0
  • 3
5 Comments
 
LVL 8

Expert Comment

by:plq
ID: 17879370
like this

#if DEBUG then
try
#endif

....


#if DEBUG then
catch ex as exception
msgbox ex.message
end try
#endif

0
 
LVL 8

Accepted Solution

by:
plq earned 500 total points
ID: 17879385
A more elegant approach might be to change the Debug>Exceptions menu so you decide which exceptions are broken into by the debugger.
0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17879547
There is one more approach to this, i do't know how you feel about it but just try
Goto Debug->Exceptions menu and in the dialog box check thrown checkbox of Common Language Runtome Exception, this will lead to break the code on the line where error occurs instead of passing it to exceptions statement.
0
 
LVL 3

Expert Comment

by:vsvb
ID: 17880256
This is the same aproch plq told about
i do this some time in  application

Declare the public boolean variable on module level or class level as Static
now when you start your application detect the status and use this flag through out your try and catch part
#If DEBUG Then
        DebugFlag = True
#End If
0
 
LVL 8

Expert Comment

by:plq
ID: 17880288
Yes, but just one minor comment... you don't want to be testing for debug code in a release build

so if you do

#If DEBUG Then
        DebugFlag = True
#End If

If DebugFlag Then
   10 lines of code
End if

Those 10 lines of code will be in your release executable even though they will never execute

So its better to do

#If DEBUG Then
   10 lines of code
#End If

#if can be broken up around constructs

so ...

#if DEBUG then
try
#end if

<lots of code to go in debug or release>


#if DEBUG then
catch ex as exception
  elh
end try
#end if
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now