Java 异常捕获错误
这是我的密码:Java 异常捕获错误,java,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;
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");
}
}
}
对我来说,这意味着它成功地添加了一个文档,同时捕获了一个异常?我真的不确定这是怎么可能的,也许我只是不完全理解异常捕获。请帮忙 您在实际成功完成文件编写之前打印了“添加的文档”
发生的情况是,您现在可能在驱动器上有文件,但它没有您期望的内容,因为您在编写HTML时似乎失败了。一旦发生异常,在try块之前不会执行任何操作
也可以考虑使用<代码> E.PrrtStAccTrace()/<代码>当您捕获异常时,您可以知道异常是什么以及它来自何处。
您应该打破像这样编写代码的习惯;通过更好地组织代码,并进行更干净的总体设计(即使是对于如此小的东西),您将发现调试更简单(或者您根本没有那么多问题)
首先,您应该输出正在捕获的异常,而不是丢弃它们。它们包含了您了解问题所在所需的确切信息 您还应该阅读您正在使用的java.io
类的javadoc;对于基于文件的东西,您真的需要关闭流和写入程序,否则您可能无法刷新缓冲区而无法写入任何内容。不关闭溪流通常是一种不好的做法
最后,我会使用函数分解将其分解为可理解的例程(将来你会很感激),或者使用有意义的变量名,而不是像
zc
和yc
之类的东西。下次,请使用101010按钮格式化你的代码。你也可以复制代码,当捕捉到异常和代码成功时。根据使用finally块。您应该避免在循环中创建PrintStream对象,并且永远不要刷新或关闭它们。如果存在一个注释,则包含所有正确的数据。为什么html代码只适用于一个用户?
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