voici mon code:Exception Catching Erreur
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");
}
}
}
Chaque fois que je lance ce je reçois la sortie suivante:
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
qui me signifie qu'il a ajouté avec succès un document et a pris une exception à le même temps? Je ne suis pas vraiment sûr de la façon dont cela est possible peut-être que je ne comprends pas l'exception attraper pleinement. S'il vous plaît aider!
La prochaine fois, s'il vous plaît formater votre code en utilisant le bouton 101010. – Thomas
Vous dupliquez également du code lorsque vous interceptez une exception et que le code réussit. Utilisez un bloc finally selon http://java.sun.com/docs/books/tutorial/essential/exceptions/finally.html –
vous devriez éviter de créer des objets PrintStream dans une boucle et ne jamais les rincer ou les fermer –