In addition, declarations are allowed at the head of for statements, as shown below:
for (int i = 0; i < 10; i++) { ... }Items declared in this way are valid only within the scope of the for statement. For example, the preceding code sample is equivalent to the following:
{ int i = 0; for (; i < 10; i++) { ... } }
a = 3; print(23); foo.bar();
if(boolean) statement else statement switch(e1) { case e2: statements default: statements } break [label]; continue [label]; return e1; for([e1]; [e2]; [e3]) statement while(boolean) statement do statement while(boolean); label:statementThe language supports labeled loops and labeled breaks, for example:
outer: // the label for (int i = 0; i < 10; i++) { for (int j= 0; j< 10; j++) { if (...) { break outer; } if (...) { } } }The use of labels in loops and breaks has the following rules:
Some exceptions are thrown by the Java runtime system. However, any class can define its own exceptions and cause them to occur using throw statements. A throw statement consists of the throw keyword followed by an object. By convention, the object should be an instance of Exception or one of its subclasses. The throw statement causes execution to switch to the appropriate exception handler. When a throw statement is executed, any code following it is not executed, and no value is returned by its enclosing method. The following example shows how to create a subclass of Exception and throw an exception.
class MyException extends Exception { } class MyClass { void oops() { if (/* no error occurred */) { ... } else { /* error occurred */ throw new MyException(); } } }To define an exception handler, the program must first surround the code that can cause the exception with a try statement. After the try statement come one or more catch statements--one per exception class that the program can handle at that point. In each catch statement is exception handling code. For example:
try { p.a = 10; } catch (NullPointerException e) { println("p was null"); } catch (Exception e) { println("other error occurred"); } catch (Object obj) { println("Who threw that object?"); }A catch statement is like a method definition with exactly one parameter and no return type. The parameter can be either a class or an interface. When an exception occurs, the nested try/catch statements are searched for a parameter that matches the exception class. The parameter is said to match the exception if it:
print("now "); try { print("is "); throw new MyException(); print("a "); } catch(MyException e) { print("the "); } print("time\n");prints "now is the time". As this example shows, exceptions don't have to be used only for error handling, but any other use is likely to result in code that's hard to understand.
Exception handlers can be nested, allowing exception handling to happen in more than one place. Nested exception handling is often used when the first handler can't recover completely from the error, yet needs to execute some cleanup code (as shown in the following code example). To pass exception handling up to the next higher handler, use the throw keyword using the same object that was caught. Note that the method that rethrows the exception stops executing after the throw statement; it never returns.
try { f.open(); } catch(Exception e) { f.close(); throw e; }
try { // do something } finally { // clean up after it }is similar to:
try { // do something } catch(Object e){ // clean up after it throw e; } // clean up after itThe finally statement is executed even if the try block contains a return, break, continue, or throw statement. For example, the following code example always results in "finally" being printed, but "after try" is printed only if a != 10.
try { if (a == 10) { return; } } finally { print("finally\n"); } print("after try\n");
ArithmeticException
Attempting to divide an integer by zero or take a modulus by zero throw the ArithmeticException--no other arithmetic operation in Java throws an exception. For information on how Java handles other arithmetic errors see Operators on Integers and Operators on Floating Point Values.
For example, the following code causes an ArithmeticException to be thrown:
class Arith { public static void main(String args[]) { int j = 0; j = j / j; } }NullPointerException
An attempt to access a variable or method in a null object or a element in a null array throws a NullPointerException. For example, the accesses o.length and a[0] in the following class declaration throws a NullPointerException at runtime.
class Null { public static void main(String args[]) { String o = null; int a[] = null; o.length(); a[0] = 0; } }It is interesting to note that if you throw a null object you actually throw a NullPointerException.
IncompatibleClassChangeException
In general the IncompatibleClassChangeException is thrown whenever one class's definition changes but other classes that reference the first class aren't recompiled. Four specific changes that throw a IncompatibleClassChangeException at rutime are:
A ClassCastException is thrown if an attempt is made to cast an object O into a class C and O is neither C nor a subclass of C. For more information on casting see Casting Between Class Types.
The following class declaration results in a ClassCastException at runtime:
class ClassCast { public static void main(String args[]) { Object o = new Object(); String s = (String)o; // the cast attempt s.length(); } }NegativeArraySizeException
A NegativeArraySizeException is thrown if an array is created with a negative size. For example, the following class definition throws a NegativeArraySizeException at runtime:
class NegArray { public static void main(String args[]) { int a[] = new int[-1]; a[0] = 0; } }OutOfMemoryException
An OutOfMemoryException is thrown when the system can no longer suppy the application with memory. The OutOfMemoryException can only occur during the creation of an object, i.e., when new is called. For example, the following code results in an OutOfMemoryException at runtime:
class Link { int a[] = new int[1000000]; Link l; } class OutOfMem { public static void main(String args[]) { Link root = new Link(); Link cur = root; while(true) { cur.l = new Link(); cur = cur.l; } } }NoClassDefFoundException
A NoClassDefFoundException is thrown if a class is referenced but the runtime system cannot find the referenced class.
For example, class NoClass is declared:
class NoClass { public static void main(String args[]) { C c = new C(); } }When NoClass is run, if the runtime system can't find C.class it throws the NoClassDefFoundException.
C.class must have existed at the time NoClass is compiled.
IncompatibleTypeException
An IncompatibleTypeException is thrown if an attempt is made to instantiate an interface. For example, the following code causes an IncompatibleTypeException to be thrown.
interface I { } class IncompType { public static void main(String args[]) { I r = (I)new("I"); } }ArrayIndexOutOfBoundsException
An attempt to access an invalid element in an array throws an ArrayIndexOutOfBoundsException. For example:
class ArrayOut { public static void main(String args[]) { int a[] = new int[0]; a[0] = 0; } }UnsatisfiedLinkException
An UnsatisfiedLinkException is thrown if a method is declared native and the method cannot be linked to a routine in the runtime.
class NoLink { static native void foo(); public static void main(String args[]) { foo(); } }InternalException
An InternalException should never be thrown. It's only thrown if some consistency check in the runtime fails. Please send mail to
java@java.Sun.COM if you have a reproducible case that throws this exception.
The Java Language Specification
Generated with CERN WebMaker