Java Reference
In-Depth Information
System.out.println( 1 / 0 );
}
}
12.16
What is displayed when the following program is run?
public class Test {
public static void main(String[] args) {
try {
method();
System.out.println( "After the method call" );
}
catch (RuntimeException ex) {
System.out.println( "RuntimeException in main" );
}
catch (Exception ex) {
System.out.println( "Exception in main" );
}
}
static void method() throws Exception {
try {
String s = "abc" ;
System.out.println(s.charAt( 3 ));
}
catch (RuntimeException ex) {
System.out.println( "RuntimeException in method()" );
}
catch (Exception ex) {
System.out.println( "Exception in method()" );
}
}
}
12.17
What does the method getMessage() do?
12.18
What does the method printStackTrace() do?
12.19
Does the presence of a try-catch block impose overhead when no exception occurs?
12.20
Correct a compile error in the following code:
public void m( int value) {
if (value < 40 )
throw new Exception( "value is too small" );
}
12.5 The finally Clause
The finally clause is always executed regardless whether an exception occurred or not.
Key
Point
Occasionally, you may want some code to be executed regardless of whether an exception
occurs or is caught. Java has a finally clause that can be used to accomplish this objective.
The syntax for the finally clause might look like this:
try {
statements;
}
catch (TheException ex) {
handling ex;
}
 
 
 
Search WWH ::




Custom Search