Solved

Conditional TRY/CATCH

Posted on 2006-11-05
5
474 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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