{
callMethod(); }
catch(Exception e) {
System.out.print(\ }
System.out.println(\ }
static void createException() {
throw new ArithmeticException(); }
static void callMethod() {
try {
createException();
System.out.print(\ }
catch(ArrayIndexOutOfBoundsException e) {
System.out.print(\ }
finally {
System.out.print(\ }
System.out.print(\ } }
8、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 class myException extends Exception{} public class Sample{ public void foo(){ try{
System.out.print(1); bar();
System.out.print(2); }catch(myException e){ System.out.print(3); } finally{
System.out.print(4); } }
public void bar() throws myException{ throw new myException(); }
public static void main(String args[]){ Sample s=new Sample(); s.foo(); } }
9、仔细阅读下面的程序,写出程序的执行顺序(写出编号): public class UsingExceptions {
public static void main( String args[] ){ try{
method1(); // 1 }catch(Exception e){
System.err.println(e.getMessage()); // 2 }
finally{
System.out.println(\ } }
public static void method1() throws Exception { method2(); //4 }
public static void method2() throws Exception { method3(); //5 }
public static void method3() throws Exception{
throw new Exception( \ } }
10、阅读下面的程序Test.java: import java.io.*; public class Test{
public static void main(String argv[]){ Test t = new Test();
System.out.println(t.fliton()); }
public int fliton(){ try{
FileInputStream din = new FileInputStream(\
din.read();
}catch(IOException ioe){
System.out.println(\ return -1; }
finally{
System.out.println(\ }
return 0; } }
如果文件test.txt与Test.java在同一个目录下,test.txt中仅有一行字符串“hello world!”,上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?
11、阅读下面的程序,写出带划线语句或注释,并写出该程序的作用: import java.io.*; public class Test {
public static void main(String args[]) { scanFiles(\ }
public static void scanFiles(String path) { if (path == null) return;
File f = new File(path); // if (!f.exists()) return;
if (f.isFile()) // System.out.println(f.getAbsolutePath()); else {
File dir[] = f.listFiles(); for (int i = 0; i < dir.length; i++)
scanFiles(dir[i].getAbsolutePath());// } } }