?
Solved

Conditional TRY/CATCH

Posted on 2006-11-05
5
Medium Priority
?
475 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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