Home All Groups Group Topic Archive Search About

Disable exceptions?

Author
14 Oct 2005 3:31 PM
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

Author
14 Oct 2005 3:41 PM
David Browne
Show quote
"Chirag" <patel***@hotmail.com> wrote in message
news:1129303861.086842.238850@g43g2000cwa.googlegroups.com...
> 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
Author
14 Oct 2005 8:11 PM
Alvin Bruney - ASP.NET MVP
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
-------------------------------------------------------



Show quote
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in
message news:egyqzWN0FHA.596@TK2MSFTNGP12.phx.gbl...
>
> "Chirag" <patel***@hotmail.com> wrote in message
> news:1129303861.086842.238850@g43g2000cwa.googlegroups.com...
> > 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
>
>
Author
17 Oct 2005 6:11 AM
Lloyd Dupont
>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 ;-)
Author
14 Oct 2005 10:50 PM
Goran Sliskovic
Show quote
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in
message news:egyqzWN0FHA.596@TK2MSFTNGP12.phx.gbl...
....
>> #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

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
Author
14 Oct 2005 11:31 PM
David Browne
Show quote
"Goran Sliskovic" <gslis***@yahoo.com> wrote in message
news:uk$KiGR0FHA.3892@TK2MSFTNGP12.phx.gbl...
>
> "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in
> message news:egyqzWN0FHA.596@TK2MSFTNGP12.phx.gbl...
> ...
>>> #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
>
> Well, this is not so uncommon concept.

In C perhaps.  And that's where it's best left.


David
Author
14 Oct 2005 11:57 PM
Goran Sliskovic
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in
message news:u7wkadR0FHA.908@tk2msftngp13.phx.gbl...
....
>>
>> Well, this is not so uncommon concept.
>
> 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
Author
15 Oct 2005 12:53 AM
David Browne
Show quote
"Goran Sliskovic" <gslis***@yahoo.com> wrote in message
news:%232VUQsR0FHA.164@TK2MSFTNGP10.phx.gbl...
>
> "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in
> message news:u7wkadR0FHA.908@tk2msftngp13.phx.gbl...
> ...
>>>
>>> Well, this is not so uncommon concept.
>>
>> 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.
>


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

David
Author
14 Oct 2005 10:44 PM
Goran Sliskovic
Show quote
"Chirag" <patel***@hotmail.com> wrote in message
news:1129303861.086842.238850@g43g2000cwa.googlegroups.com...
> 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
Author
15 Oct 2005 6:41 AM
Jon Skeet [C# MVP]
Chirag <patel***@hotmail.com> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
17 Oct 2005 6:15 AM
Lloyd Dupont
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
    }
}

Show quote
"Chirag" <patel***@hotmail.com> wrote in message
news:1129303861.086842.238850@g43g2000cwa.googlegroups.com...
> 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
>

AddThis Social Bookmark Button