【发布时间】:2017-08-07 20:48:03
【问题描述】:
我正在使用 C# windows 窗体创建一个工具,它可以快速检查我们公司内部订购网站上的订单状态。我正在尝试处理 Web 请求在尝试打开新 IE 窗口时引发的格式化异常。
我的工具添加了 8 位用户输入并将其添加到 URL 以获取用户特定订单并打开一个新的 IE 窗口以显示它。
private void scRequestBtn_Click(object sender, EventArgs e)
{
try
{
var scUrl = "http://iwha.mycompany.com/servicesdashboard/InstanceInfoRedirect.aspx?instance_id=";
var scInput = scUrl += scTextBox.Text;
Process.Start("IExplore.exe", scInput);
}
catch (FormatException)
{
MessageBox.Show("Your entry is not valid. Please verify the ID number");
}
}
如果输入了 8 位数字以外的信息(例如,如果用户键入字符串而不是整数),我会得到一个带有服务器错误的 IE 窗口。它有一个堆栈跟踪如下:
[FormatException: Input string was not in a correct format.] Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String Value, NumberFormatInfo NumberFormat) +203 Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +73
[InvalidCastException: Conversion from string "gsdfgsdfgsfg" to type 'Long' is not valid.] Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +328 ServiceDashboard_41.InstanceInfoRedirect.Page_Load(Object sender, EventArgs e) +86 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
我觉得托管订购网站的内部服务器引发的异常与您看到的典型异常不同。我读过“肥皂”异常可能就是所谓的,但我并不真正了解如何处理。
我的问题是你将如何处理程序之外的东西引发的异常?
【问题讨论】:
-
垃圾进,垃圾出。清理用户输入以确保其符合他们应该输入的内容。
标签: c# web-services exception-handling