Savitch 9.1-9.3
1
2
3
4
5
6
7
8
9
10
11
12
13
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of donuts:");
int donutCount = keyboard.nextInt();
System.out.println("Enter number of glasses of milk:");
int milkCount = keyboard.nextInt();
if (milkCount < 1)
System.out.println("No milk! Go buy some milk"); // can't divide by zero
else {
System.out.print("You have " + donutCount / (double)milkCount)
System.out.println(" donuts for each glass of milk. ";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Scanner keyboard = new Scanner(System.in);
try {
System.out.println("Enter number of donuts:");
int donutCount = keyboard.nextInt();
System.out.println("Enter number of glasses of milk:");
int milkCount = keyboard.nextInt();
if (milkCount < 1)
throw new Exception("Exception: No milk!");
System.out.print("You have " + (donutCount / (double)milkCount))
System.out.println(" donuts for each glass of milk. ";
}
catch(Exception e) {
System.out.println(e.getMessage());
System.out.println("Go buy some milk.");
}
try-throw-catch
approach to exceptions.
1
try { Code_To_Try }
throw
keyword.
1
throw new Exception(Message);
try
block it can be handled by a catch block.
1
catch (Exception e) { Code_To_Handle_Exception }
try
block, the rest of the try
block is skipped and controls jumps to the corresponding catch
block.
1
2
3
4
catch(Exception e) { // e is the catch-block parameter
System.out.println(e.getMessage());
System.out.println("Go buy some milk.");
}
try
block, the catch
block is skipped and control jumps to the code immediately after the catch
block.IOExpectation
or ClassNotFoundExpectation
.try
block.catch
block should catch the specific exception.
1
catch (IOException e)
getMessage
method to generate a string message.
1
System.out.println(e.getMessage());
catch
block can call System.exit
to end the program.Exception
class.
1
2
3
4
5
6
7
8
public class DivideByZeroException extends Exception {
public DivideByZeroException() {
super("Divideing by Zero!"); // calls the Exception constructor
}
public DivideByZeroException(String message) {
super(message); // calls the Exception constructor
}
}
1
throw new DivideByZeroException();
Exception
class defines a getMessage
method that returns the string passed to the Exception
constructor.getMessage
is inherited by any class that extends Exception
, and should not be overridden.getMessage
works, use super
to call the Exception
constructor in all derived class constructors.throws
clause in the header.
1
public void sampleMethod() throws DivideByZeroException
sampleMethod
must do one of the following:
try
block with a catch
block that handles a DivideByZeroException
.throws
caluse for DivideByZeroException
in its own header.throws
caluse can contain multiple exception types:
1
public int myMethod() throws IOException, DivideByZeroException
throws
clause it can not add expectations to throws clause, but it can remove them.throws
Clause
1
2
3
4
5
6
7
8
9
10
11
12
13
public void doDivision() throws DivideByZeroException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numerator:");
int numerator = keyboard.nextInt();
System.out.println("Enter denominator:");
int denominator = keyboard.nextInt();
if (denominator == 0)
throw new DivideByZeroException();
double quotient = numerator / (double)denominator;
System.out.println(numerator + "/" + denominator + "=" + quotient);
}
throws
clause.
IOException
, ClassNotFoundException
,NoSuchMethodException
and BadStringOperationException
are checked exceptions.ArrayIndexOutOfBoundsException
, NullPointerException
, and ArithmeticException
are unchecked exceptions.RunTimeException
class.Exception
class derives from the Throwable
class.Error
class, which is also derives from the Throwable
class.throws
clause.OutOfMemoryError
occurs if your program runs out memory.AssertionError
occurs if an assertion check fails.try
block can throw multiple exceptions of possibly different types.catch
block can catch
exceptions of only one type, but multiple catch
blocks can follow a try
block
1
2
3
4
5
6
7
8
9
10
try {
// code that may throw a NegativeNumberException
// code that may throw a DivideByZeroException
}
catch(DivideByZeroException e) {
// handle DivideByZeroException
}
catch(NegativeNumberException e) {
// handle NegativeNumberException
}
1
2
3
catch(Exception e) { ... } // handles all exceptions
catch(DivideByZeroException e) { ... } // never reached
// the order of the catch clauses should be reversed
try-catch
blocks, but it is usually not necessary and should be avoided.finally
Blockfinally
block is always executed whether or not an exception is thrown or caught.finally
block must appear after all catch
blocks.
1
2
3
4
5
6
7
8
9
try {
// code to try executing
}
catch(Exception e) {
// handle an exception
}
finally {
// always executed
}
Exception
. Descendants of the class Error are not exceptions, but they behave like them.throws
clause within its heading. Checked exceptions must be caught eventually. Otherwise, program execution will terminate. Unchecked, or run-time, exceptions need not be caught or declared in a throws clause and usually are not. Unchecked exceptions belong to classes derived from the class RuntimeException
. All other exceptions are checked.throw
statement.throws
clause for the method.catch
block.try
block is followed by one or more catch blocks. In this case, always list the catch block for a more specific exception class before the catch
block for a more general exception class.getMessage
method that can be used to recover a description of the caught exception.