try
{
int.Parse(this.userName.Text);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
与
try
{
int.Parse(this.userName.Text);
}
catch (Exception ex)
{
throw new Exception(ex.Message,ex);
}
前者
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Exception: Input string was not in a correct format.
at WinFormHello.Form1.button1_Click(Object sender, EventArgs e) in X:\VS2010Item\Test\WinFormHello\WinFormHello\Form1.cs:line 43
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
后者
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Exception: Input string was not in a correct format. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at WinFormHello.Form1.button1_Click(Object sender, EventArgs e) in X:\VS2010Item\Test\WinFormHello\WinFormHello\Form1.cs:line 38
--- End of inner exception stack trace ---
at WinFormHello.Form1.button1_Click(Object sender, EventArgs e) in X:\VS2010Item\Test\WinFormHello\WinFormHello\Form1.cs:line 43//抛出错误的行号
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
多了带颜色的这些,可以很清楚的知道是在程序那里出了问题
但是一般情况下,我们不想让用户知道详细的出错信息,这可能会潜在风险
而我们最常用的,可能会是
try {
}
catch(SQLException){
//do something not using exception info
}
catch(OtherException ex){
//do something else using exception info
}
catch(Exception ex){
//do something else
Logger.Log(ex);
MessageBox.Show("Code:001 "+ex.Message,"Error");
}
提供一个错误代码,然后程序员再去查文档,确定是那里的问题.再去查代码
又或者会在 throw new Exception("ECode:001 " + ex.Message);前再加一段代码,把相关的错误信息,存入数据库.但是这种方式,也有不好的地方,就是你得有数据库的使用权限,但是一般情况下,交付后的系统,你将是没有这个权限的.只能是对方把这些信息截取出来,发送给开发商.