Java Reference
In-Depth Information
System.out.println(“Inside method2”);
NotSoLazy util = new NotSoLazy(fileName);
System.out.println(util.readOneByte());
System.out.println(“Leaving method2”);
}
}
So how come main() does not need to declare that it throws an ArrayIndex-
OutOfBoundsException? Because it is a runtime exception, and runtime excep-
tions are not affected by the Handle or Declare Rule (only checked exceptions
are).
By the way, I used an index of 5 just to demonstrate that the value of 5 is a
part of the message that is displayed when the ArrayIndexOutOfBounds-
Exception occurs. Figure 11.8 shows the output of the ThrowDemo program
when no command-line argument is entered.
Figure 11.9 shows the output of the ThrowDemo program when the file can-
not be found by the readOneByte() method.
The throws keyword is used for declaring an exception, and it is used only
in method signatures. The throw keyword is for throwing an exception,
and it can be used anywhere you want to throw an exception.
Figure 11.8
The main() method threw a new ArrayIndexOutOfBoundsException.
Figure 11.9
The readOneByte() method threw a FileNotFoundException.
Search WWH ::




Custom Search