C# provides the facility to handle exceptions using the try and catch block. There are two ways -- you can either use throw(ex) method or the simpler throw method, as below.
First Way
- try
- {
- }
- catch(Exception ex)
- {
- throw;
- }
Second way
- try
- {
- }
- catch(Exception ex)
- {
- throw(ex);
- }
Now, the question arises -- which one is better?
Here is the screenshot of a simple program.
The Throw method throws the current exception only while the Throw(ex) method will reset your stack trace so the error will appear from the line where Throw(ex) is written. Since Throw does not reset the stack trace, you will get the information about the original exception. Throw (ex) gives the following Exception Message.
at ConsoleApp1.Program.Main(String[] args) in D:\INTW\Console\ConsoleApp1\ConsoleApp1\Program.cs:line 19
And, Throw sends this Exception Message.
at ConsoleApp1.Program.DevideByZero(Int32 i) in D:\INTW\Console\ConsoleApp1\ConsoleApp1\Program.cs:line 26
at ConsoleApp1.Program.Main(String[] args) in D:\INTW\Console\ConsoleApp1\ConsoleApp1\Program.cs:line 19
So, you can see in the above image, we have got full stack trace information where the actual exception is called at line 26 and again rethrown at line 19.
While in the first screenshot, you can see there is only information about line 19. It does not show a full-stack trace. So, I will recommend you to always use Throw method.