C# 异常抛出写法不同引发的问题

第一种: 

            try
            {
                var s = Convert.ToInt16("dd"); //17行
            }
            catch (Exception ex)
            {
                throw  ex;
            }

直接抛出的异常是:

“System.FormatException”类型的未经处理的异常在 Common.exe 中发生 

其他信息: 输入字符串的格式不正确。

进入到ex.StackTrace

   在 Common.Program.Main(String[] args) 位置 e:\woke\整理\Common\Common\Program.cs:行号 21
   在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()

第二种(resharper建议删除catch模块):

            try
            {
                var s = Convert.ToInt16("dd"); //17行
            }
            catch (Exception ex)
            {
                throw   ;
            }

直接抛出的异常是:

“System.FormatException”类型的未经处理的异常在 Common.exe 中发生 

其他信息: 输入字符串的格式不正确。

和第一种一样的

进入到ex.StackTrace:

   在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Convert.ToInt16(String value)
 
 在 Common.Program.Main(String[] args) 位置 e:\woke\整理\Common\Common\Program.cs:行号 21
   在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()

 

第三种(resharper建议删除catch模块)::

            try
            {
                var s = Convert.ToInt16("dd"); //17行
            }
            catch 
            {
                throw   ;
            }

直接抛出的异常是:

“System.FormatException”类型的未经处理的异常在 Common.exe 中发生 

其他信息: 输入字符串的格式不正确。

在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Convert.ToInt16(String value)

   在 Common.Program.Main(String[] args) 位置 e:\woke\整理\Common\Common\Program.cs:行号 21
   在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()

第四种:

            try
            {
                var s = Convert.ToInt16("dd");  //17行
            }
            catch (Exception ex)
            {
                throw  new Exception(ex.ToString()) ;
            }

直接抛出的异常:

“System.Exception”类型的未经处理的异常在 Common.exe 中发生 

其他信息: System.FormatException: 输入字符串的格式不正确。

   在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

   在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

   在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)

   在 System.Convert.ToInt16(String value)

   在 Common.Program.Main(String[] args) 位置 e:\woke\整理\Common\Common\Program.cs:行号 17

 

如此看来只有第四种才是真正直接抛出错误点在哪里,其他的都是将原始错误点清除掉然后抛出新的异常

转载于:https://my.oschina.net/objectboy/blog/746650

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值