异常捕获错误

时间:2010-06-23 17:31:26

标签: java

这是我的代码:

import java.util.*;
import java.io.*;
import java.lang.*;

public class Home {

    public static void main(String[] args) {
        FileOutputStream home,file,filein,fileinin;
        int x = 0;
        int y = 0;
        int yc = 0;
        int z = 0;
        int zc = 0;
        //TestCase
        String Annotation0 = "Hello";
        String Annotation1 = "World";
        String Annotation2 = "How Are You";
        String Annotation3 = "Today?";
        String Annotation4 = "Fine";
        String Annotation5 = "Thanks";
        List<List> corpus0 = new ArrayList<List>();
        List<List> corpus1 = new ArrayList<List>();
        List<String> document0 = new ArrayList<String>();
        List<String> document1 = new ArrayList<String>();
        List<String> document2 = new ArrayList<String>();
        List<String> document3 = new ArrayList<String>();
        List<List<List>> biglist = new ArrayList<List<List>>();
        biglist.add(corpus0);
        biglist.add(corpus1);
        corpus0.add(document0);
        corpus0.add(document1);
        corpus1.add(document2);
        corpus1.add(document3);
        document0.add(Annotation0);
        document1.add(Annotation1);
        document2.add(Annotation2);
        document2.add(Annotation3);
        document3.add(Annotation4);
        document3.add(Annotation5);

        try{
            home = new FileOutputStream("C:\\Windows\\Temp\\Home.html");
            new PrintStream(home).printf("%s", "<html>\n <body>\n <h1> Home </h1> \n");
            System.out.println("Home Created Successfully");
        while (x<biglist.size()){
            yc=0;
            try {
                file = new FileOutputStream ("C:\\Windows\\Temp\\Corpus"+x+".html");
                System.out.println("Corpus Added");
                new PrintStream(home).printf("%s%s%s%s%s", "<A href=\"/C:/Windows/Temp/Corpus",x,".html\">Corpus ",x,"</A><Br>\n");
                new PrintStream(file).printf("%s%s%s","<html>\n <body>\n <h1> Corpus ", x,"</h1> \n");
                while (yc<biglist.get(x).size()){
                    zc=0;
                    try{
                        filein = new FileOutputStream ("C:\\Windows\\Temp\\Document"+y+".html");
                        System.out.println("Document Added");
                        new PrintStream(filein).printf("%s%s%s","<html>\n <body>\n <h1> Document ", y,"</h1> \n");
                        new PrintStream(file).printf("%s%s%s%s%s", "<A href=\"/C:/Windows/Temp/Document",y,".html\">Document ",y,"</A><Br>\n");
                        while (zc<biglist.get(x).get(y).size()){
                            try{
                                fileinin = new FileOutputStream ("C:\\Windows\\Temp\\Annotation"+z+".html");
                                System.out.println("Annotation Added");
                                new PrintStream(fileinin).printf("%s%s%s%s%s","<html>\n <body>\n <h1> Annotation ", z,"</h1> \n <p>",biglist.get(x).get(y).get(z),"</p> \n </body> \n </html>");
                                new PrintStream(filein).printf("%s%s%s%s%s", "<A href=\"/C:/Windows/Temp/Annotation",z,".html\">Annotation ",z,"</A><Br>\n");
                                z++;
                                zc++;}                      
                            catch(Exception e) {System.out.println("Error Annotating");
                                z++;
                                zc++;}}
                        new PrintStream(filein).printf("%s","</body> \n</html>");
                        y++;
                        yc++;}
                    catch(Exception e) {
                        System.out.println("Error Making Document");
                        y++;
                        yc++;}}
                new PrintStream(file).printf("%s","\n</body> \n</html>");
            x++;}
            catch(Exception e) {
                System.out.println("Error Making Corpus");
                x++;
             }
        }
        new PrintStream(home).printf("%s","\n</body> \n</html>");}
        catch (Exception e){
            System.out.println("Fatal Error Home Creation Failed");
        }


    }

}

每次运行时我都会得到以下输出:

Home Created Successfully
Corpus Added
Document Added
Annotation Added
Document Added
Annotation Added
Error Annotating
Corpus Added
Document Added
Error Making Document
Document Added
Error Making Document

对我来说意味着它成功添加了一个文档并同时捕获了异常?我真的不确定这是怎么可能的,也许我只是不明白异常捕获。请帮忙!

2 个答案:

答案 0 :(得分:0)

在您成功写完文件之前,您已经打印了“添加的文档”。

您可能现在可能在驱动器上有该文件,但它没有您期望的内容,因为您在HTML编写过程中似乎失败了。一旦发生异常,在try块之前不会执行任何操作。

另外,考虑在捕获异常时使用e.printStackTrace(),以便了解异常是什么以及来自何处。

答案 1 :(得分:0)

你应该打破写这样代码的习惯;通过更好地构建代码,并拥有整体更清晰的设计(即使是如此小的东西),您会发现调试更简单(或者您根本没有那么多问题)。

首先,您应该输出您正在捕获的异常而不是丢弃它们。它们包含您需要的完全信息,以了解出现了什么问题。

您还应该阅读您正在使用的java.io类的javadoc;对于基于文件的东西,你真的需要关闭流和编写器,否则你可能无法刷新缓冲区而且没有任何内容写入。不关闭流是一种不好的做法。

最后,我要么使用功能分解将其分解为可理解的例程(将来你会感恩)或使用有意义的变量名而不是zcyc之类的东西。

相关问题