How can I use the try catch block such that if this statement fails xml <- xmlTreeParse(xmlTxt, useInternal=TRUE) then this statement is executed xml <- xmlMalFormed() ? This code does not work but assuming its somewhere along these lines: tryCatch(xml <- xmlTreeParse(xmlTxt, useInternal=TRUE), xml <- xmlMalFormed(f1)) -- View this message in context: http://www.nabble.com/try-catch-block-tp17938467p17938467.html Sent from the R help mailing list archive at Nabble.com.
ppatel3026 <pratik.patel at us.rothschild.com> writes:> How can I use the try catch block such that if this statement fails > xml <- xmlTreeParse(xmlTxt, useInternal=TRUE) > > then this statement is executed > > xml <- xmlMalFormed()library(XML) xml <- tryCatch({ # other code, then... xmlTreeParse("sdf", useInternal=TRUE) }, error=function(err) { cat("err:", conditionMessage(err), "\n") ## etc "oops" }) xml # "oops"> ??tryCatch has examples.> This code does not work but assuming its somewhere along these lines: > tryCatch(xml <- xmlTreeParse(xmlTxt, useInternal=TRUE), xml <- > xmlMalFormed(f1))Martin> -- > View this message in context: http://www.nabble.com/try-catch-block-tp17938467p17938467.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M2 B169 Phone: (206) 667-2793
This is a useful reference on the tryCatch() function: http://www1.maths.lth.se/help/R/ExceptionHandlingInR/ I think something like this should work: xml <- tryCatch(xmlTreeParse(xmlTxt, useInternal=TRUE), error=function(err) xmlMalFormed()) ----- Original Message ---- From: ppatel3026 <pratik.patel at us.rothschild.com> To: r-help at r-project.org Sent: Tuesday, June 17, 2008 2:15:43 PM Subject: [R] try catch block How can I use the try catch block such that if this statement fails xml <- xmlTreeParse(xmlTxt, useInternal=TRUE) then this statement is executed xml <- xmlMalFormed() ? This code does not work but assuming its somewhere along these lines: tryCatch(xml <- xmlTreeParse(xmlTxt, useInternal=TRUE), xml <- xmlMalFormed(f1)) -- View this message in context: http://www.nabble.com/try-catch-block-tp17938467p17938467.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.