Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Do you use this keyword or throw some validation runtime exception? What benefits it gives to you or why you think it's not worth to use? Thanks.
Effective Java (Second Edition), Item 4, discusses using private constructors to enforce noninstantiability. Here's the code sample from the book: public final class UtilityClass { private UtilityClass() { throw new AssertionError(); } } However, AssertionError doesn't seem like the right thing to throw. Nothing is being "asserted", which is how the API defines the use of Ass...
Java's checked exceptions sometimes force you to catch a checked exception that you believe will never be thrown. Best practice dictates that you wrap that in an unchecked exception and rethrow it, just in case. What exception class do you wrap with in that case? What exception would you wrap with in the "// Should never happen" case?
most of the time i will use exception to check for condition in my code, i wonder when is appropriate time to use assertion for instance, Group group=null; try{ group = service().getGroup("abc"); }catch(Exception e){ //i dont log error because i know whenever error occur mean group not found } if(group !=null) { //do something } 1.can comment on this code and how assertion fit in here ...
I want to throw a runtime exception in case my class invariants are invalidated. Since this is a programming error (similar to a NullPointerException), clients should not catch that exception. Should the exception class be declared private or public (or something else)? class Foo { // ... private static class InvariantsViolated { // ... } } Are there any guidelines ...
What exactly happens when a Java assertion fails? How does the programmer come to know that an assertion has failed? Thanks.
In Java, I will occasionally throw an AssertionError directly, to assert that a particular line will not be reached. An example of this would be to assert that the default case in a switch statement cannot be reached (see this JavaSpecialists page for an example). I would like to use a similar mechanism in .Net. Is there an equivalent exception that I could use? Or is there another method tha...
i am reading assertions in java.i have material and i had seen some e - material too about assertions.But i am not able to get the main theme why we are using assertions and what it will do.can any one explain to me in general statements?
In the class I am testing, there are a few assert statements that check for various conditions. One of the methods is GetNames(string id){ assert(! id.Equals("")); // Causes all junit tests to stop ... } and there is an assert statement to check if id is not blank. In my unit tests, I have one test where I pass it a blank string, and a few others. The problem is that when the ass...
  /*
   * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang;

Thrown to indicate that an assertion has failed.

The seven one-argument public constructors provided by this class ensure that the assertion error returned by the invocation:

     new AssertionError(expression)
 
has as its detail message the string conversion of expression (as defined in The Java Language Specification, Second Edition, Section 15.18.1.1), regardless of the type of expression.

Since:
1.4
 
 public class AssertionError extends Error {
    
Constructs an AssertionError with no detail message.
 
     public AssertionError() {
     }

    
This internal constructor does no processing on its string argument, even if it is a null reference. The public constructors will never call this constructor with a null argument.
 
     private AssertionError(String detailMessage) {
         super(detailMessage);
     }

    
Constructs an AssertionError with its detail message derived from the specified object, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

If the specified object is an instance of Throwable, it becomes the cause of the newly constructed assertion error.

Parameters:
detailMessage value to be used in constructing detail message
See also:
Throwable.getCause()
 
     public AssertionError(Object detailMessage) {
         this("" +  detailMessage);
         if (detailMessage instanceof Throwable)
             initCause((ThrowabledetailMessage);
     }

    
Constructs an AssertionError with its detail message derived from the specified boolean, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
 
     public AssertionError(boolean detailMessage) {
         this("" +  detailMessage);
     }

    
Constructs an AssertionError with its detail message derived from the specified char, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
 
     public AssertionError(char detailMessage) {
        this("" +  detailMessage);
    }

    
Constructs an AssertionError with its detail message derived from the specified int, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
    public AssertionError(int detailMessage) {
        this("" +  detailMessage);
    }

    
Constructs an AssertionError with its detail message derived from the specified long, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
    public AssertionError(long detailMessage) {
        this("" +  detailMessage);
    }

    
Constructs an AssertionError with its detail message derived from the specified float, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
    public AssertionError(float detailMessage) {
        this("" +  detailMessage);
    }

    
Constructs an AssertionError with its detail message derived from the specified double, which is converted to a string as defined in The Java Language Specification, Second Edition, Section 15.18.1.1.

Parameters:
detailMessage value to be used in constructing detail message
    public AssertionError(double detailMessage) {
        this("" +  detailMessage);
    }
New to GrepCode? Check out our FAQ X