
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to rethrow InnerException without losing stack trace in C#?
In c#, the throw is a keyword and it is useful to throw an exception manually during the execution of the program and we can handle those thrown exceptions using try−catch blocks based on our requirements.
By using throw keyword in the catch block, we can re-throw an exception that is handled in the catch block. The re-throwing an exception is useful when we want to pass an exception to the caller to handle it in a way they want.
Following is the example of re−throwing an exception to the caller using throw keyword with try-catch blocks in c#.
Example
class Program{ static void Main(string[] args){ try{ Method2(); } catch (System.Exception ex){ System.Console.WriteLine($"{ex.StackTrace.ToString()} {ex.Message}"); } Console.ReadLine(); } static void Method2(){ try{ Method1(); } catch (System.Exception){ throw; } } static void Method1(){ try{ throw new NullReferenceException("Null Exception error"); } catch (System.Exception){ throw; } } }
This is how we can re-throw an exception to the caller using throw keyword in catch block based on our requirements.
Output
at DemoApplication.Program.Method1() in C:\Users\Koushik\Desktop\Questions\ConsoleApp\Program.cs:line 49 at DemoApplication.Program.Method2() in C:\Users\Koushik\Desktop\Questions\ConsoleApp\Program.cs:line 37 at DemoApplication.Program.Main(String[] args) in C:\Users\Koushik\Desktop\Questions\ConsoleApp\Program.cs:line 24 Null Exception error
- Related Articles
- Print stack trace in Java
- How to get stack trace using thread in Java 9?
- Place Stack Trace into a String in Java
- How to clear table formatting style without losing table data in Excel?
- Java Program to Convert a Stack Trace to a String
- How can I get a stack trace for a JavaScript exception?
- How to sort an R data frame column without losing row names?
- How to rethrow an exception in Java?\n
- How to reload the current page without losing any form data with HTML?
- How to change the column position of MySQL table without losing column data?
- How can I get a JavaScript stack trace when I throw an exception?
- How to rethrow Python exception with new type?
- Find maximum in stack in O(1) without using additional stack in C++
- Is there a way to convert Integer field into Varchar without losing data in MySQL?
- Postorder traversal of Binary Tree without recursion and without stack in C++

Advertisements