throw new Exception

            try
            {
                int.Parse(this.userName.Text);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);

.NET中的Exception处理(C#)


            }

            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);前再加一段代码,把相关的错误信息,存入数据库.但是这种方式,也有不好的地方,就是你得有数据库的使用权限,但是一般情况下,交付后的系统,你将是没有这个权限的.只能是对方把这些信息截取出来,发送给开发商.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值