Can I ignore the exception by mistake?
how to ignore exceptions in c #?
Not necessary.
Exceptions are trying to tell you that there is something wrong with your code.
If you get an exception while parsing XML because a specific element doesn't exist, rewrite your code to check if that element exists before trying to read it.
If you are asking how to handle a specific exception, 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 will handle the specific case where the XML is incorrect. However, if the XML is well-formed, but your code throws an exception because a certain element does not exist, and you assumed it would, then don't use exception handling code at all; just make sure it exists before trying to read it.
a source to share
Never ignore exceptions. These "I'm sure I can safely ignore this" sometimes cause the worst and harder to find bugs.
Always log the exception at a minimum . Alternatively, also use a method Debug.Fail
that displays a dialog box detailing the exception in your debug build - this will help you better diagnose most problems.
In this particular case (XML parsing) there is certainly a way to deal with situations where data is not found gracefully, without accounting for and ignoring exceptions - feel free to edit the answer to show us which XML parsing method you are using and we can tell you how to avoid exceptions in the first place.
Since you may be asking about the generic case, this is how you deal with exceptions if you want to "ignore" them (if you don't want 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());
}
a source to share
Okay, to answer your question, if you put your code in a Try Catch block and then for the exception in question, just catch it and do nothing.
That being said, why not just check for a null tag during handling rather than relying on exception handling? Exception handling has more overhead than state checking, and if there is an expected case where something might not exist, you should handle that case in your code.
a source to share
If the exception is remote , as you say, and you only get a string representation of your message or result, then you won't be able to use the exception handling mechanisms. However, you could flatten something together where you recognize the lines and do something.
If you get an Exception object, you can pull the good old try / catch file out of it .
a source to share
Use a try-catch block. http://msdn.microsoft.com/en-us/library/0yd65esw%28VS.80%29.aspx
a source to share