java - Prevent code from throwing XML exception -


in following piece of code :

try{ .... }      catch(exception e)     {             logger.error("exception caught in method: \n");             logger.error(e.getmessage());             string fault = "<fault><faultcode>" + e.getclass().getname() + "</faultcode><faultdescription>" + e.getmessage() + "</faultdescription></fault>";             return xmlobject.factory.parse(fault);     } 

, there way convert "fault" string xml object using code wouldn't throw exception or require exception handling?

basically, need return xml object in both success , fauilure scenarios, without method using throws clause. that's reason, we've removed specific exception cases, occur in main flow, parserconfiguration, ioexceptoin, xmlexception etc. catch , kept generic 1 , hard coded string gather required fault info , pass xml object. but, minimal coding, there chance of xmlexception occuring in parse() method. other way, reading errorconfig file or creating separate xml object bring in more exceptions handled.

so, want know whether possible develop such code, i.e. exception-free catch block returning xml? or, if recommend other approach fine.

fyi : main code transforms xml or enriches appending more tags , returns it.

hope i've made myself clear.

if parser cannot parse xml (and aborts exception), going xml object from?

if think in flow of operation no xml exception can ever happen (which compiler of course cannot guarantee), assume that, catch exception, log "oops, should never have happened", re-throw runtimeexception , don't worry longer (it won't ever happen, right?)

string fault = "<fault><faultcode>" + e.getclass().getname() +    "</faultcode> <faultdescription>" + e.getmessage()     + "</faultdescription></fault>"; return xmlobject.factory.parse(fault); 

i think (if error message not need escaping), can sure never fail. fit above pattern.

i don't know if "checked-exception-free", forgo xml string , re-parsing , construct dom programmatically instead.

  documentbuilderfactory dbf = documentbuilderfactory.newinstance();   document doc = dbf.newdocumentbuilder().newdocument();   element rootelement = doc.createelement("fault");   // etc ... 

i don't know if "checked-exception-free",

looking @ javadocs: nope. kinds of configuration exceptions. :-(


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -