AssertionError.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* AssertionError.java -- indication of a failed assertion
  2. Copyright (C) 2002, 2005, 2012 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. /**
  33. * An assertion error normally occurs as a result of the <code>assert</code>
  34. * statement added in JDK 1.4, to indicate that an assertion failed. There
  35. * are enough constructors to ensure that
  36. * <code>new AssertionError(<em>expression</em>)</code> will work for all
  37. * expressions, regardless of type, as if the error message were given by
  38. * the string <code>"" + <em>expression</em></code>. This extends Error,
  39. * because you usually do not want to inadvertently trap an assertion failure.
  40. *
  41. * @author Eric Blake (ebb9@email.byu.edu)
  42. * @since 1.4
  43. * @status updated to 1.4
  44. */
  45. public class AssertionError extends Error
  46. {
  47. /**
  48. * Compatible with JDK 1.4+.
  49. */
  50. private static final long serialVersionUID = -5013299493970297370L;
  51. /**
  52. * Construct an AssertionError with no detail message.
  53. */
  54. public AssertionError()
  55. {
  56. }
  57. /**
  58. * Construct an AssertionError with the string conversion of the given
  59. * object as its error message. If the object is a Throwable, it is also
  60. * set as the cause of this error.
  61. *
  62. * @param msg the source of the error message
  63. * @see Throwable#getCause()
  64. */
  65. public AssertionError(Object msg)
  66. {
  67. super("" + msg);
  68. if (msg instanceof Throwable)
  69. initCause((Throwable) msg);
  70. }
  71. /**
  72. * Construct an AssertionError with the string conversion of the given
  73. * boolean as its error message.
  74. *
  75. * @param msg the source of the error message
  76. */
  77. public AssertionError(boolean msg)
  78. {
  79. super(msg ? "true" : "false");
  80. }
  81. /**
  82. * Construct an AssertionError with the string conversion of the given
  83. * char as its error message.
  84. *
  85. * @param msg the source of the error message
  86. */
  87. public AssertionError(char msg)
  88. {
  89. super(String.valueOf(msg));
  90. }
  91. /**
  92. * Construct an AssertionError with the string conversion of the given
  93. * int as its error message.
  94. *
  95. * @param msg the source of the error message
  96. */
  97. public AssertionError(int msg)
  98. {
  99. super(Integer.toString(msg, 10));
  100. }
  101. /**
  102. * Construct an AssertionError with the string conversion of the given
  103. * long as its error message.
  104. *
  105. * @param msg the source of the error message
  106. */
  107. public AssertionError(long msg)
  108. {
  109. super(Long.toString(msg));
  110. }
  111. /**
  112. * Construct an AssertionError with the string conversion of the given
  113. * float as its error message.
  114. *
  115. * @param msg the source of the error message
  116. */
  117. public AssertionError(float msg)
  118. {
  119. super(Float.toString(msg));
  120. }
  121. /**
  122. * Construct an AssertionError with the string conversion of the given
  123. * double as its error message.
  124. *
  125. * @param msg the source of the error message
  126. */
  127. public AssertionError(double msg)
  128. {
  129. super(Double.toString(msg));
  130. }
  131. /**
  132. * Construct an AssertionError with detail message and cause.
  133. *
  134. * @param msg Detail message.
  135. * @param cause The cause of this exception, may be null
  136. * @since 1.7
  137. */
  138. public AssertionError(String msg, Throwable cause)
  139. {
  140. super(msg, cause);
  141. }
  142. }