Java Reference
In-Depth Information
valueOf methods of all the wrapper classes in java.lang . For example, the following code
generates a NumberFormatException :
3. String s = “hello”;
4. int x = Integer.parseInt(s);
The string “hello” is clearly not an integer, so attempting to parse it into an int
generates the following stack trace:
Exception in thread “main” java.lang.NumberFormatException: For input string:
“hello”
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at NumberFormatDemo.main(NumberFormatDemo.java:4)
AssertionError
An AssertionError is thrown to indicate an assertion has failed. You do not throw an
AssertionError programmatically. The JVM throws one when assertions are enabled and
the boolean expression of an assert statement is false .
ExceptionInInitializerError
An ExceptionInInitializer is thrown by the JVM when an unexpected exception occurs
during a static initializer or the initializer of a static variable. Here is a simple example that
demonstrates this exception:
1. public class ExceptionInInitializerDemo {
2. static {
3. Integer x = null;
4. x.intValue();
5. }
6.
7. public static void main(String [] args) {
8. System.out.println(“Inside main”);
9. }
10. }
A NullPointerException occurs on line 4, which causes an ExceptionInInitializerError
to be thrown. Running this program generates the following stack trace:
Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at ExceptionInInitializerDemo.<clinit>(ExceptionInInitializerDemo.java:4)
Search WWH ::




Custom Search