exception.md 3.2 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
# 异常

已知我们有一个特殊的文件类型 PopFile,这个类型的对象方法 read() 可以返回字符串
数据,当读取到末尾的时候,会抛出 Eof 异常,我们希望先通过 PopFile.open(path)
新建一个对象,并从这样一个 PopFile 对象中读取最多一百个文本数据,然后调用 close 
方法关闭它。返回字符串列表`List<String>`。但是这个对象中的数据可能不足一百个。
此时我们需要在末尾添加一项 `[stop]`

下列选项正确的是:

## 答案

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        try{
            for(int i=0; i<100; i++){
                result.add(popFile.read());
            }
        } catch(EofException eof){
            result.add("[stop]");
        } finally {
            popFile.close();
        }
        return reseult;
    }
}
```

## 选项

### A

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        try{
            for(int i=0; i<100; i++){
                result.add(popFile.read());
            }
            return result;
        } catch(EofException eof){
            result.add("[stop]");
            return result;
        } 
        popFile.close();
    }
}
```

### B

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        try{
            while(true){
                result.add(popFile.read());
            }
        } catch(EofException eof){
            if(result.size() < 100){
                result.add("[stop]");
            }
            return result;
        } finally {
            popFile.close();
        }
    }
}
```

### C

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        try{
            for(int i=0; i<100; i++){
                result.add(popFile.read());
            }
            return result;
        } catch(EofException eof){
            result.add("[stop]");
            return result;
        } finally {
            popFile.close();
        }
    }
}
```

### D

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        for(int i=0; i<100; i++){
            result.add(popFile.read());
        }
        if(result.size() < 100){
            result.add("[stop]");
        }
        popFile.close();
        return result;
    }
}
```

### E

```java
public class App{
    public List<String> Top100(String path){
        PopFile popFile = PopFile.open(path);
        List<String> result = new ArrayList();
        try{
            for(int i=0; i<100; i++){
                result.add(popFile.read());
            }
            if(result.size() < 100){
                result.add("[stop]");
            }
            return result;
        } catch(Excption error){
        } finally {
            popFile.close();
        }
        return result;
    }
}
```