Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditional TRY/CATCH

Posted on 2006-11-05
5
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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