Solved

Conditional TRY/CATCH

Posted on 2006-11-05
5
468 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

12 Experts available now in Live!

Get 1:1 Help Now