Throwing - Causing an exception to occur.
Catching - Capturing an exception that has just occurred, and executing statements to resolve it in some way.
Catch Clause - The block that does this.
Stack Trace - The sequence of call statements that brought control to the method where the exception happened.
throw
statement, or implicitly by carrying out some illegal or invalid action.
throw
 ExceptionObject
java.lang.Exception
.
class melon { public static void main(String args[]) { int i = 1, j = 0, k; k = i / j; // causes division-by-zero error } }
% javac melon.java % java melon java.lang.ArithmeticException: / by zero at melon.main (melon.java: 5)
ArithmeticException
, known as runtime exceptions.
class OutofGas extends Exception {} class banana { ... if (fuel < 0.1) throw new OutofGas(); }
try
/catch
statement.
[catch (arg) block]
[finally block]
try
statement says "try these statements, and see if you
get an exception".
try
statement must be followed by at least one (you can
have many) catch
clause and possibly the finally
clause.
finally
clause.
catch
says "I will handle any exception that matches
my argument".
finally
block, if present is a "last chance to clean up"
block.
try
block
and can contain the housekeeping tasks that must always be done when finishing
this piece of code.
try
/catch
statement:
int a[] = new int [5]; try { a[10] = 100; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array index is out of bounds"); }
catch
clauses match the exception that has
been thrown (or even if there was a match), the finally
clause is executed (if there is one).
try
statement at all. The flow of control abrubtly leaves this
method, and a premature return is done to the method that called this one.
try
statement itself,
then we look for a matching exception again, and so on.
modifiers_and_returntype name (params) throws e1, e2, e3 {}
e1
,...etc. must be exception or error names (i.e.
any type that is assignable to the predefined type Throwable
).
char readChar() throws IOException;
int carrot[];
int carrot[256]; // Wrong!
int carrot[] = new int[256];
int wine[] = {1, 2, 3, 4, 5, 6}; float beer[] = {5.5F, p, q, 2.2F};
a.length; // the correct way
a.length(); // Wrong!
String
in the array:
public static void main(string args[]) { int i = 0; System.out.println("number of Strings: " + args.length); System.out.println("length of i'th string: " +args[i].length()); }
length
is a data field for arrays, and is a method
for the class String
.
int day[] = new int[356];
day
are in the range of 0 to 364.
arraycopy
method:
System.arraycopy(source, sourceStart, destination, destStart, length);
names
:
String [] temVar = new String[2 * names.length]; System.arraycopy(names, 0, temVar, 0, names.length); names = temvar;
String
, StringBuffer
, and
char[]
.
String
using pieces from the
String
you have.
+
operator to concatenate Strings you
are actually creating a new String
object.
String
class supports:
String
out of other types including
StringBuffer
and array of char
equals
method can be used to compare Strings.
equals
method (which is inherited by class
String
from its superclass Object
) is used to test
any two objects for equality (i.e., the contents of the two objects are
identical).
true
if the objects are equal and
false
otherwise.
s1 = new String( "hello" ); ... // test for equality if ( s1.equals( "hello" ) ) System.out.println("s1 equals \"hello\""); else System.out.println("s1 does not equal \"hello\"");
equals
is
true
and therefore the first statement will be printed.
equals
functionality
with the ==
operator. They are not the same.
==
, the result is
true
if both references refer to the same object in memory.
s1 == "hello"
evaluates to false
.
String
method compareTo
to
compare String
objects.
s1 = new String( "hello" ); s2 = new String( "good bye" ); ... s1.compareTo( s2 )
String s1
to String s2
.
Method compareTo
returns 0 if the Strings are equal, a negative
number if the String
that invokes compareTo
is less
than the String
that is passed as an argument, and a positive
number if the String
that invokes compareTo
is
greater than the String
that is passed as an argument.
compareTo
uses lexicographical comparison. The value
returned is the difference between the integer representations of the first
character that is different in each String
.
compareTo
will return 1
because the integer representation of 'h' is 104 and the integer representation
of 'g' is 103.
String
class, you
are strongly encouraged to read about them (as well as the methods for
StringBuffer
and char[]
).
StringBuffer
class supports:
StringBuffer
StringBuffer
String
or StringBuffer
does require a method call:
String s = "hello"; int i = s.length();
new
operator is allowed when giving
Strings an initialvalue.
String s = "drinking";
String s = new String("drinking");