Three ways to throw exception in C#. Which is your preference?
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
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 . 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 )
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.