The basic difference is that the Throw exception overwrites the stack trace and this makes it hard to find the original code line number that has thrown the exception.
Throw basically retains the stack information and adds to the stack information in the exception that it is thrown.
Let us see what it means rather speaking so many words to better understand the differences. I am using a console application to easily test and see how the usage of the two differ in their functionality.
Throw basically retains the stack information and adds to the stack information in the exception that it is thrown.
Let us see what it means rather speaking so many words to better understand the differences. I am using a console application to easily test and see how the usage of the two differ in their functionality.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace TestingThrowExceptions {
- class Program {
- public void ExceptionMethod() {
- throw new Exception("Original Exception occurred in ExceptionMethod");
- }
- static void Main(string[] args) {
- Program p = new Program();
- try {
- p.ExceptionMethod();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
- }
No comments:
Post a Comment