how do i ignore exceptions in c#?
i am reading a remote xml file, and sometimes the tag doesnt exist and it throws an exception. how do i ignore the exception and goto ne开发者_JS百科xt?
how do i ignore exceptions in c#?
Don't.
Exceptions are trying to tell you that something is wrong with your code.
If you're getting an exception during XML parsing because a specific element doesn't exist, then rewrite the code to check whether or not that element exists before trying to read it.
If you are asking how to handle a specific exception, then you can wrap it in a try-catch block:
XmlDocument doc = new XmlDocument();
try
{
doc.Load(url);
}
catch (XmlException)
{
// Handle the error here
return (default value);
}
// Start going through the XmlDocument
The above code would handle the specific case where the XML is malformed. However, if the XML is well-formed but your code is throwing an exception because a particular element does not exist and you assumed that it would, then don't use exception-handling code at all; simply check that it exists before trying to read it.
try
{
yourXmlDocument.LoadXML("xml is here");
}catch{
//It has failed.
}
You could be even more specific and only catch errors from LoadXML
try
{
yourXmlDocument.LoadXML("xml is here");
}catch(System.Xml.XmlException e){
//It has failed.
}
Well, to answer you question, if you put your code in a Try Catch block and then for the exception in question, simply catch it and do nothing.
That being said, why not just check for a null tag while processing rather than rely on exception handling? Exception handling has more overhead generally than checking for a condition and if there is an expected case where something may not exist, you should handle that case in your code.
Never ever ignore exceptions. These "I am sure I can safely ignore this" sometimes cause the worst and hardest to find bugs.
Always at least log the exception. Optionally, also use Debug.Fail
method, which will display a dialog with exception details in your debug build - this will help you to diagnose most problems much better.
In this particular case (XML parsing), there surely is a way how to handle cases when the data is not found gracefully, without catching and ignoring exceptions - feel free to edit the answer to show us what exact xml parsing method you are using and we can tell you how to avoid the exceptions in the first place.
Since you may be asking about a generic case, this is how you deal with exceptions if you want to "ignore" them (if you do not wish to display them to the user):
try
{
//do something that throws an exception
}
catch(Exception ex) //or even better: catch a specific exception type
{
//do not ignore the exception, at least log it
System.Diagnostics.Debug.Fail(ex.Message, ex.ToString());
log.Debug("Probably expected error happened: " + ex.ToString());
}
You can simply put a try/catch around the code and do nothing with the exception that is generated.
If the exception is remote, like you say, and you're only getting the string version of its message or result, then you won't be able to use exception handling mechanisms on it. However you might be able to cobble something together where you recognize the strings and do something.
If you get the Exception object then you can pull out the good old try/catch stuff on it.
Use a try-catch block. http://msdn.microsoft.com/en-us/library/0yd65esw%28VS.80%29.aspx
精彩评论