Re: Assert vs Exceptions

Discussion in 'Java' started by Roedy Green, Aug 14, 2003.

  1. Roedy Green

    Roedy Green Guest

    On 14 Aug 2003 12:29:36 -0700, (Doug Pardee)
    wrote or quoted :

    >Never use an assert for something that could happen, because the
    >assertions can be turned off, and probably WILL be turned off in
    >production use.


    The nice thing about assertions is that they can be turned back on in
    the field for problem tracking.

    This is what primarily distinguishes them from

    static final boolean DEBUG = true;

    if (DEBUG)
    {
    }

    logic.

    The JVM actually somehow removes them if they are not being turned on.
    I got the impression it is something cleverer than

    if ( assertionsEnabled )
    {
    assert ( allMenAreCreatedEqual() );
    }
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 14, 2003
    #1
    1. Advertising

  2. Roedy Green

    Wayne Guest

    Roedy Green wrote:

    >
    > The nice thing about assertions is that they can be turned back on in
    > the field for problem tracking.
    >
    > This is what primarily distinguishes them from
    >
    > static final boolean DEBUG = true;
    >
    > if (DEBUG)
    > {
    > }
    >
    > logic.
    >
    > The JVM actually somehow removes them if they are not being turned on.
    > I got the impression it is something cleverer than
    >
    > if ( assertionsEnabled )
    > {
    > assert ( allMenAreCreatedEqual() );
    > }


    I believe the JVM strips the assertion bytecodes from the .class
    files when loading them into RAM. This means that assertions have
    little overhead on a production system, just the slight extra
    disk space (and loading time).

    -Wayne
     
    Wayne, Aug 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    387
    Roedy Green
    Aug 14, 2003
  2. Robert Brewer
    Replies:
    1
    Views:
    518
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,563
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    957
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,753
    Richard Bos
    May 17, 2010
Loading...

Share This Page