10.3.1.1. Exceptions Matching.
Solution.
Integer.parseInt("26.2"); ==> NumberFormatException
String s; s.indexOf('a'); ==> NullPointerException
String s = "hello"; s.charAt(5); ==> StringIndexOutOfBoundsException
catch block | catch an exception | checked exception |
dialog box | dynamic scope | error dialog |
exception | exception handling | finally block |
method call stack | method stack trace | modal dialog |
static scope | throw an exception | try block |
unchecked exception |
try/catch/finally
statement has the following syntax:try {
// Block of statements
// At least one of which may throw an exception
if ( * Some condition obtains */ )
throw new ExceptionName();} catch (ExceptionName ParameterName) {
// Block of statements to be executed
// If the ExceptionName exception is thrown in try}
..} catch (ExceptionName2 ParameterName) {
// Block of statements to be executed
// If the ExceptionName2 exception is thrown in try
} finally {
// Optional block of statements that is executed
// Whether an exception is thrown or not
}
throw
statement inside the try block is there to illustrate how throw
can be used. You will usually not see a throw
statement in a try block, because most throws are done from within Java library methods, which are called from a try
block.throw
an Exception
, which is caught by special code known as an exception handler. A throw
statement—throw new Exception()
—is used to throw an exception.throws
the exception.RuntimeException
. If they are left uncaught, they will be handled by Java’s default exception handlers.try/catch
syntax allows you to separate the normal parts of an algorithm from special code meant to handle errors and exceptional conditions.Exception.printStackTrace()
method can be called by exception handlers to print a trace of exactly how the program reached the statement that threw the exception. Static scoping refers to how the text of the program is arranged. If a variable is declared within a method or a block, its static scope is confined to that method or block. Dynamic scoping refers to how the program is executed. A statement is within the dynamic scope of a method or block if it is called from that method or block, or if it is called by some other method that was called from that method or block.throw
statements do not appear in the program. For example, Java’s integer division operator will throw an ArithmeticException
if an attempt is made to divide by zero.finally
statement is an optional part of a try/catch
block. Statements contained in a finally block will be executed whether an exception is raised or not.Exception
class or one of its subclasses.Integer.parseInt("26.2"); ==> NumberFormatException
String s; s.indexOf('a'); ==> NullPointerException
String s = "hello"; s.charAt(5); ==> StringIndexOutOfBoundsException
IndexOutOfBoundsException
, NumberFormatException
, and NullPointerException
, because these are subclasses of RuntimeException
. The others are checked exceptions.ArrayIndexOutOfBoundsException
could be handled by the handlers in RunTimeException
, IndexOutOfBoundsException
, or Exception, because their classes are all superclasses of ArrayIndexOutOfBoundsException
.Math.random()
in MyClass2
returns 0.98 and then 0.44, the program will generate the following output: 0.98 is out of range
method1()
, method2()
is not called at all. The following stack trace would be printed: java.lang.ArithmeticException: 0.98 is out of range
at MyClass2.method1(MyClass2.java:3)
at MyClass2.main(MyClass2.java:15)
Math.random()
in MyClass2
returns 0.44 and then 0.98, the program will generate the following output: Hello 0.44
0.98 is out of range
java.lang.ArithmeticException: 0.98 is out of range
at MyClass2.method2(MyClass2.java:8)
at MyClass2.main(MyClass2.java:16)
BadDivide
occurs in the expression n/d
in Method2()
. It would generate the following stack trace: java.lang.ArithmeticException: divide by zero
at BadDivide.method2(BadDivide.java:7)
at BadDivide.method1(BadDivide.java:3)
at BadDivide.main(BadDivide.java:13)
BadDivide.method2()
will handle the divide-by-zero error itself: public void method2 (int n, int d) {
try {
System.out.println(n / d);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(0);
}
}
someValue
equals 1000, the code segment will print Entering try block
ERROR: 1000 is too large
someValue
equals 50, the code segment will print Entering try block
Exiting try block
try {
if (X < 0)
throw new Exception("ERROR: Negative value in X coordinate");
} catch (Exception e) {
System.out.println( e.getMessage() );
}
public class FieldIsEmptyException extends Exception {
public FieldIsEmptyException () {
super("The input field is empty ");
}
}
public int getInt() {
int num = 0;
try {
String data = getText();
if (data.equals(""))
throw new FieldIsEmptyException();
num = Integer.parseInt( getText() );
if (num > bound)
throw new IntOutOfRangeException(bound);
} catch (FieldIsEmptyException e) {
System.out.println("Error: " + e.getMessage() );
} catch (NumberFormatException e) {
System.out.println("Error: You must input an integer.
Please try again.");
} catch (IntOutOfRangeException e) {
System.out.println(e.getMessage());
return 0;
}
return num;
}