Disable exceptions?

  • Thread starter Thread starter Chirag
  • Start date Start date
C

Chirag

Is there a master define that I can toggle to disable exceptions? I
would rather not use asserts. I would also like to avoid the following
clunky syntax:


#If DEBUG Then
If array.Count <> m_numDimensions Then
Throw New ApplicationException("Invalid number of dimensions")
End If
#End If

Any elegant techniques out there that won't clutter code?

Thanks,
Chirag
 
Chirag said:
Is there a master define that I can toggle to disable exceptions? I
would rather not use asserts. I would also like to avoid the following
clunky syntax:


#If DEBUG Then
If array.Count <> m_numDimensions Then
Throw New ApplicationException("Invalid number of dimensions")
End If
#End If

Any elegant techniques out there that won't clutter code?

So it's ok to have an invalid number of dimentions in production, but not
when you're debugging?

David
 
I have used this approach with some degree of success. To disable
exceptions, unplug the machine from the socket. At this point, the software
cannot possible throw any exceptions.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Chirag said:
Is there a master define that I can toggle to disable exceptions? I
would rather not use asserts. I would also like to avoid the following
clunky syntax:


#If DEBUG Then
If array.Count <> m_numDimensions Then
Throw New ApplicationException("Invalid number of dimensions")
End If
#End If

Any elegant techniques out there that won't clutter code?
....

Well, you could make a class with public static method (C# code):
class DebugCheck {

public static void Check(bool assertion, Exception e) {
#if DEBUG
if (!assertion) {
throw e;
}
#endif
}

then you could use:
DebugCheck.Check( array.Count == m_numDimensions, new
ApplicationException("Invalid number of dimensions"));

You could also do:

[ Conditional("Debug") ]
public static void Check(bool assertion, Exception e) {
if (!assertion) {
throw e;
}
}

Regards,
Goran
 
message ....
So it's ok to have an invalid number of dimentions in production, but not
when you're debugging?

David

Well, this is not so uncommon concept. Some runtime checks are expensive and
you don't won't to run them in production (after it is thoroughly tested),
but while debbuging you want to catch it immediatly. That does not mean that
ultimately exception will not be thrown at some other place later, but
rather you added extra checks during execution path to support debugging.

Though programmers should be very careful on what can be disabled at
runtime.

Regards,
Goran
 
message ....
In C perhaps. And that's where it's best left.
....
No, it is alive and well in many languages (C++/C#/Java/Eiffel etc, just
look up "design by contract" on google). I'm not going to argue whether it's
good technique or whether all checks should also be done at release builds
because this is off-topic here, but it does exists.

Regards,
Goran
 
Goran Sliskovic said:
message ...
...
No, it is alive and well in many languages (C++/C#/Java/Eiffel etc, just
look up "design by contract" on google). I'm not going to argue whether
it's good technique or whether all checks should also be done at release
builds because this is off-topic here, but it does exists.


Enforcing the contract is good. Turning it off without a very good reason
is bad.

David
 
Chirag said:
Is there a master define that I can toggle to disable exceptions? I
would rather not use asserts. I would also like to avoid the following
clunky syntax:


#If DEBUG Then
If array.Count <> m_numDimensions Then
Throw New ApplicationException("Invalid number of dimensions")
End If
#End If

Any elegant techniques out there that won't clutter code?

Well, Debug.Assert doesn't actually thrown an exception, but it
wouldn't be hard to write your own method that *did* throw an
exception:

<Conditional("DEBUG")>
Overloads Public Shared Sub Assert( _
ByVal condition As Boolean, _
ByVal message As String
)
Begin Sub
If Not condition Then
Throw New AssertionFailedException(message)
End If
End Sub

(with an appropriate AssertionFailedException, of course).

Then you'd just call:

Foo.Assert (array.Count = m_numDimensions, _
"Invalid number of dimensions")

I must say, I'm more in the "throw the exception even in production"
camp myself though.
 
I have used this approach with some degree of success. To disable
exceptions, unplug the machine from the socket. At this point, the
software
cannot possible throw any exceptions.
Damn, this is clever!
I should advice the same approach to user fo my products ;-)
 
there is a very easy way, using conditional attribute.
BTW the Debug class use conditional attribute, so use it as much as you
want, it won't be called in production.
Much the same way

like this
using System.Diagnostic;
class AClass
{
[Conditional("DEBUG")]
public void DoSomeTest(object objToBeTested)
{
if(skyIsBlue & objToBeTested != null)
throw new StupidUserException();
}

void MyMethod()
{
DoSomeText(); // <= only called in debug mode, same as an #if DEBUG
#endif pair
}
}
 
Back
Top