开发者

Exception Catching Error

开发者 https://www.devze.com 2023-01-04 07:24 出处:网络
here\'s my code: import java.util.*; import java.io.*; import java.lang.*; public class Home { public static void main(String[] args) {

here's my code:

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");
        }


    }

}

Every time I run this I get the following output:

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

which to me means that it successfully added a document and caught an exception at the same time? I'm really not sure how this is possible maybe i just don't understand exception catching开发者_开发技巧 fully. Please help!


You should break the habit of writing code like this; by better structuring your code, and having an overall cleaner design (even for something so small), you will find debugging much simpler (or that you simply don't have as many problems).

Primarily, you should output the exceptions that you are catching rather than throwing them away. They contain the exact information you need to understand what's going wrong.

You should also read the javadoc for the java.io classes you are using; for file-based stuff, you really need to close the streams and writers or you risk not flushing buffers and nothing getting written. It is generally a bad practice to not close streams.

Lastly, I would either use functional decomposition to break this up into comprehensible routines (future you will be thankful) or use meaningful variable names and not stuff like zc and yc.


You printed "added document" before you actually finished writing the file successfully.

What happens is that you probably now have the file on the drive, but it doesn't have the content you expected since you seem to be failing during the HTML writing. Once an exception occurs, nothing will execute until the try block.

Also, consider using an e.printStackTrace() when you catch the exception so you can have an idea what the exception is and where it comes from.

0

精彩评论

暂无评论...
验证码 换一张
取 消