Three ways to throw exception in C#. Which is your preference?
2015-11-02 18:45
411 查看
There are three ways to 'throw' a exception in C# C#中有三种抛出异常的方式
Use the throw keyword without an identifier 直接使用throw关键字
Use the throw keyword with the original exception 使用throw关键字抛出捕获的异常对象
Use the throw keyword with a new exception 使用throw关键字抛出一个自定义的对像
The first option will rethrow the exception without modifying the call stack. This option should be used when you don’t want any modifications to the exception
第一种方法会直接把当前捕获异常抛出,并保留原始异常的stacktrace
In this example, the ex.StackTrace contains the information came from the original exception. So you can debug the error easier. The stacktrace is
If you are working on debug environment, the complier gives you a clarified information about in which line the exception is threw.
When you choose the second option, you reset the call stack to the current location in code
In this example, the ex.StackTrace is
Compared the result with the previous one, you should find that line 34 is disappeared. But the two examples are almost same except‘throw ex’in the second example and ‘throw’ in the first example in the same line. So you can’t see where the exception originally came from. It is terrible for a developer who has been so crazy in dealing with the modification because of the changed requirements
![](http://images2015.cnblogs.com/blog/472352/201511/472352-20151102183814399-1759508153.png)
. But in C# 5, there is a new feature to give you a additional option to throw a exception and preserve the original stack trace. You can use ExceptionDispatchInfo.Throw (another syntactic sugar or something like that? Whatever encapsulated or wrapper methods are always useful
![](http://images2015.cnblogs.com/blog/472352/201511/472352-20151102183815024-1674474153.png)
)
Using the third option can be useful when you want to raise another exception to the caller of your code. Nothing to say about it because it is very easy to be understood. You can define a custom exception inherited System.Exception and set the inner exception to the original exception.
Use the throw keyword without an identifier 直接使用throw关键字
Use the throw keyword with the original exception 使用throw关键字抛出捕获的异常对象
Use the throw keyword with a new exception 使用throw关键字抛出一个自定义的对像
The first option will rethrow the exception without modifying the call stack. This option should be used when you don’t want any modifications to the exception
第一种方法会直接把当前捕获异常抛出,并保留原始异常的stacktrace
class Program { static void Main(string[] args) { try { new Test().Raise(); } catch(Exception ex) { throw; } } } public class Test { public void Raise() { throw new Exception("11111"); } }
In this example, the ex.StackTrace contains the information came from the original exception. So you can debug the error easier. The stacktrace is
at ConsoleApplication1.Test.Raise() in d:\Project\Test\ConsoleApplication1\ConsoleApplication1\Program.cs:line 34 at ConsoleApplication1.Program.Main(String[] args) in d:\Project\Test\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20
If you are working on debug environment, the complier gives you a clarified information about in which line the exception is threw.
When you choose the second option, you reset the call stack to the current location in code
class Program { static void Main(string[] args) { try { new Test().Raise(); } catch(Exception ex) { throw ex; } } } public class Test { public void Raise() { throw new Exception("11111"); } }
In this example, the ex.StackTrace is
at ConsoleApplication1.Program.Main(String[] args) in d:\Project\Test\ConsoleApplication1\ConsoleApplication1\Program.cs:line 26 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
Compared the result with the previous one, you should find that line 34 is disappeared. But the two examples are almost same except‘throw ex’in the second example and ‘throw’ in the first example in the same line. So you can’t see where the exception originally came from. It is terrible for a developer who has been so crazy in dealing with the modification because of the changed requirements
![](http://images2015.cnblogs.com/blog/472352/201511/472352-20151102183814399-1759508153.png)
. But in C# 5, there is a new feature to give you a additional option to throw a exception and preserve the original stack trace. You can use ExceptionDispatchInfo.Throw (another syntactic sugar or something like that? Whatever encapsulated or wrapper methods are always useful
![](http://images2015.cnblogs.com/blog/472352/201511/472352-20151102183815024-1674474153.png)
)
Using the third option can be useful when you want to raise another exception to the caller of your code. Nothing to say about it because it is very easy to be understood. You can define a custom exception inherited System.Exception and set the inner exception to the original exception.
相关文章推荐
- pjsip之PJNATH简介(1)
- JavaScript中的Map
- 节点的nodeName与nodeValue
- uva 1218 Perfect Service 树形dp
- Elasticsearch Javascript API增删改查
- jsp 关于jdbc驱动;
- 使用Jquery解析xml的两种方法
- jQuery插件DataTable删除行后的页面更新利
- Handlebars 和 SeaJS 的结合使用
- js
- js在网页中判断横屏竖屏
- [转]Web安全之JSP详解
- 正则表达式匹配(递归+剑指offer)
- React Native for Android 研究总结
- BootStrap学习笔记-5
- 利用JavaScript实现的2048游戏
- JS添加tr序号
- C程序员的JavaScript学习笔记
- js根据身份证计算年龄
- mime导致zabbix web页面css样式不能显示的问题