Still working too hard
Here's another example of someone working too hard ...
try { // Load the XML string into an XmlTextReader XDocument xmlFile = XDocument.Parse(inputXmlString); string prettyXml = xmlFile.ToString(SaveOptions.None); editXml.Text = prettyXml; success = true; } catch (Exception exception) { if (exception.GetType() == typeof(XmlException)) { ShowMessage("The File is not a valid format. Format should be XML", "Invalid XML File", MessageType.Error, MessageButton.OK); } else { throw; } success = false; }
Here, the developer is trying to only take action on a specific kind of exception. The trick here is that C# includes native support for this kind of thing. The (simpler) code that works as required is this:
try { // Load the XML string into an XmlTextReader XDocument xmlFile = XDocument.Parse(inputXmlString); string prettyXml = xmlFile.ToString(SaveOptions.None); editXml.Text = prettyXml; success = true; } catch (XmlException exception) { ShowMessage("The File is not a valid format. Format should be XML", "Invalid XML File", MessageType.Error, MessageButton.OK); success = false; }
The goal here is to only catch the exception you need, let the others propagate normally.
This kind of error seems to be common with developers with backgrounds involving tools (such as class Visual basic) where error handling much coarser than C#.
The key lesson to be learnt here isn't about exception handling, it is this: Habits shouldn't always be carried forward with you from platform to platform - good practice in one may not be good practice in the next, especially if that practice was to work around limitations in the platform that may no longer exist.
Recent comments
3 weeks 4 days ago
4 weeks 3 days ago
4 weeks 3 days ago
4 weeks 4 days ago
7 weeks 16 hours ago
7 weeks 3 days ago
7 weeks 3 days ago
10 weeks 1 day ago
11 weeks 3 days ago
11 weeks 4 days ago