Can I ignore the exception by mistake?

how to ignore exceptions in c #?

I am reading a remote xml file and sometimes the tag doesn't exist and throws an exception. how can i ignore the exception and move on?

+2


a source to share


7 replies


try
{
 yourXmlDocument.LoadXML("xml is here");
}catch{
 //It has failed.
}

      

You can be more specific and only catch errors from LoadXML



try
{
    yourXmlDocument.LoadXML("xml is here");
}catch(System.Xml.XmlException e){
    //It has failed.
}

      

+2


a source


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.

+4


a source


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());
}

      

+1


a source


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.

+1


a source


You can just put try / catch around the code and do nothing with the exception thrown.

0


a source


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 .

0


a source


0


a source







All Articles