|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Disable exceptions?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
Show quote
"Chirag" <patel***@hotmail.com> wrote in message So it's ok to have an invalid number of dimentions in production, but not 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? > 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. -- Show quoteRegards, 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 ------------------------------------------------------- "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 > > >I have used this approach with some degree of success. To disable Damn, this is clever!> exceptions, unplug the machine from the socket. At this point, the > software > cannot possible throw any exceptions. > I should advice the same approach to user fo my products ;-)
Show quote
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in Well, this is not so uncommon concept. Some runtime checks are expensive and 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 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
Show quote
"Goran Sliskovic" <gslis***@yahoo.com> wrote in message In C perhaps. And that's where it's best left.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. David "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in No, it is alive and well in many languages (C++/C#/Java/Eiffel etc, just 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. > .... 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
Show quote
"Goran Sliskovic" <gslis***@yahoo.com> wrote in message Enforcing the contract is good. Turning it off without a very good reason 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. > is bad. David
Show quote
"Chirag" <patel***@hotmail.com> wrote in message Well, you could make a class with public static method (C# code):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? .... 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 Chirag <patel***@hotmail.com> wrote:
> Is there a master define that I can toggle to disable exceptions? I Well, Debug.Assert doesn't actually thrown an exception, but it > 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? 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 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 > |
|||||||||||||||||||||||