Java Reference
In-Depth Information
Java syntax: throw-statement
throw throwable-object ;
Example : throw new NumberFormatException();
Purpose : Terminate normal execution and “throw” the throwable-
object , which must be an instance of class Throwable. Unless the
thrown object is caught, execution terminates with a message.
In the first case, you have to find out why your program used too much
memory. In the other two cases, which should rarely occur, it is difficult to say
what to do. Something caused things to become really messed up. Perhaps
recompiling all files may help.
Message printed for thrown Error s and Exception s are similar.
10.2
The throw-statement
Many exceptions and errors are thrown by the Java system itself. But you can
write your own statements to “throw an exception”. For example, consider this
little program:
Activity
10-4.1
public class Ex {
public static void main(String[] args) {
throw new ArithmeticException("/ by zero");
}
}
public class Throwable implements ... {
private transient Object backtrace;
private String detailMessage;
/** Constructor: an instance with no detail message */
public Throwable() { ... }
/** Constructor: an instance with detail message m */
public Throwable(String m) { ... }
/** = the detail message ( null if none) */
public String getMessage() { ... }
/** = localized message. If not overridden, same as getMessage() */
public String getLocalizedMessage() { ... }
/** = short description of this instance */
public String toString() { ... }
}
Figure 10.2: Class Throwable (not all methods are shown)
Search WWH ::




Custom Search