Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Essential Java Classes
Lesson: Exceptions

Questions and Exercises问题和练习

Questions问题

  1. Is the following code legal?以下代码合法吗?合法
    try {
        
    } finally {
        
    }
  2. What exception types can be caught by the following handler?以下处理程序可以捕获哪些异常类型?
    catch (Exception e) {
         
    }
    What is wrong with using this type of exception handler?使用这种类型的异常处理程序有什么问题?
  3. Is there anything wrong with the following exception handler as written? Will this code compile?以下编写的异常处理程序是否有任何错误?这段代码可以编译吗?
    try {
    
    } catch (Exception e) {
        
    } catch (ArithmeticException a) {
        
    }
  4. Match each situation in the first list with an item in the second list.将第一个列表中的每个情况与第二个列表中的一个项目进行匹配。
    1. int[] A;
      A[0] = 0;
    2. The JVM starts running your program, but the JVM can't find the Java platform classes.JVM开始运行您的程序,但JVM找不到Java平台类。(The Java platform classes reside in classes.zip or rt.jar.)(Java平台类位于classes.ziprt.jar中。)
    3. A program is reading a stream and reaches the end of stream marker.程序正在读取流并到达end of stream标记。
    4. Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again.在关闭流之前和到达end of stream标记之后,程序尝试再次读取流。
    1. __error
    2. __checked exception
    3. __compile error
    4. __no exception

    Exercises练习

    1. Add a readList method to ListOfNumbers.java.readList方法添加到ListOfNumbers.javaThis method should read in int values from a file, print each value, and append them to the end of the vector.这个方法应该从文件中读取int值,打印每个值,并将它们附加到向量的末尾。You should catch all appropriate errors. You will also need a text file containing numbers to read in.您应该捕获所有适当的错误。您还需要一个包含要读取的数字的文本文件。
    2. Modify the following cat method so that it will compile.修改以下cat方法,使其能够编译。
      public static void cat(File file) {
          RandomAccessFile input = null;
          String line = null;
      
          try {
              input = new RandomAccessFile(file, "r");
              while ((line = input.readLine()) != null) {
                  System.out.println(line);
              }
              return;
          } finally {
              if (input != null) {
                  input.close();
              }
          }
      }
    Check your answers.检查你的答案。

Previous page: Summary
Next page: Basic I/O