How to continue execution after a throw exception in Java?

tnaser picture tnaser · Jan 25, 2012 · Viewed 12.6k times · Source

I am reading HTML files from a folder using xml parser that store the clean code in tagNode

try {
    Document doc = new DomSerializer(props, true).createDOM(tagNode);
} catch (Exception ex) {
 ex.printStackTrace();
}

But one of the files is giving me an error :

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 

How I can continue running the program after the exception is caught ?


solution #1

    try 
    {
            File folder = new File(path);
            File[] listOfFiles = folder.listFiles();
        FileWriter fstream = new FileWriter("dataset.txt");
            BufferedWriter br= new BufferedWriter(fstream);


for (int i = 0; i < listOfFiles.length; i++) {    
{
        try {
            Document doc = new DomSerializer(props, true).createDOM(tagNode);
        } catch (Exception ex) {
         ex.printStackTrace();
        }
    }
    } catch (Exception ex) {
         ex.printStackTrace();
        }

Given a way around it , why I am getting this error ?

Answer

Shawn picture Shawn · Jan 25, 2012

Use a try/catch block

try{
   Document doc = new DomSerializer(props, true).createDOM(tagNode);
}
catch(DOMException e){
   //error handling here if you want
}

 //we now hit more code