Better exception handling

This commit is contained in:
~lucidiot 2022-06-19 04:59:14 +02:00
parent 319f241939
commit a118b37259
2 changed files with 21 additions and 20 deletions

View File

@ -1,8 +1,9 @@
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
namespace ToeCracker {
public partial class MainForm : Form {
@ -45,22 +46,23 @@ namespace ToeCracker {
this.Enabled = false;
Dictionary<string, string[]> results = new Dictionary<string,string[]>();
results = Thesaurus.Search(query);
try {
// results = Thesaurus.Search(query);
} catch (Exception e) {
// TODO: Maybe specialize the exception handler?
// TODO: Internationalize the error dialog box
string message = "An error occurred while searching the thesaurus:\n\n" + e.ToString();
MessageBoxButtons buttons = MessageBoxButtons.OK;
// When a debugger is attached, offer to throw the exception again, because we are just handling System.Exception here which is very broad.
if (Debugger.IsAttached) {
message += "\n\nA debugger appears to be attached. Would you like to enter it?";
buttons = MessageBoxButtons.YesNo;
}
if (MessageBox.Show(message, "Thesaurus lookup failed", buttons, MessageBoxIcon.Error) == DialogResult.Yes && Debugger.IsAttached)
throw e;
results = Thesaurus.Search(query);
} catch (WebException e) {
MessageBox.Show(
Properties.Resources.RequestError + "\n\n" + e.ToString(),
Properties.Resources.RequestErrorTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
// No early return: we let the below code handle a "no results" case with the default empty dictionary.
} catch (XmlException e) {
MessageBox.Show(
Properties.Resources.ParsingError + "\n\n" + e.ToString(),
Properties.Resources.ParsingErrorTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
this.tabControl.TabPages.Clear();

View File

@ -46,9 +46,8 @@ namespace ToeCracker {
request.UserAgent = String.Format("{0}/{1}", Application.ProductName, Application.ProductVersion);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// TODO: Better exception types!
if ((int)response.StatusCode < 200 || (int)response.StatusCode > 299)
throw new Exception(String.Format("Unexpected HTTP status code {0}", response.StatusCode));
throw new WebException(String.Format("Unexpected HTTP status code {0}", response.StatusCode));
XmlDocument doc = new XmlDocument();
@ -68,7 +67,7 @@ namespace ToeCracker {
break;
}
if (line == null)
throw new Exception("Result block not found.");
throw new XmlException("Result block not found.");
while ((line = reader.ReadLine()) != null) {
sb.AppendLine(line);
@ -76,7 +75,7 @@ namespace ToeCracker {
break;
}
if (line == null)
throw new Exception("End of result block not found.");
throw new XmlException("End of result block not found.");
doc.LoadXml(sb.ToString());
}
@ -113,7 +112,7 @@ namespace ToeCracker {
groupName = node.InnerText;
} else if (node.Name.ToLowerInvariant() == "ul") {
if (groupName == null)
throw new Exception("Unexpected word list with no group name");
throw new XmlException("Unexpected word list with no group name.");
foreach (XmlNode listItem in node.ChildNodes) {
if (listItem.NodeType == XmlNodeType.Element && listItem.Name == "li")
words.Add(listItem.InnerText);