
- 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
Chained Exceptions in C#
Chained Exceptions are a chain of try-catch statements that handle exceptions. To create a chain of exceptions i.e. chained exceptions −
Set the first try-catch −
Example
static void Main(string[] args) { try { One(); } catch (Exception e) { Console.WriteLine(e); } }
Now try-catch under method One() −
Example
static void One() { try { Two(); } catch (Exception e) { throw new Exception("First exception!", e); } }
The method Two() also continues chained exception.
Example
static void Two() { try { Three(); } catch (Exception e) { throw new Exception("Second Exception!", e); } }
Now the next method.
Example
static void Three() { try { Last(); } catch (Exception e) { throw new Exception("Third Exception!", e); } }
The takes us to the last.
Example
static void Last() { throw new Exception("Last exception!"); }
On running the above, the exceptions would be handled like this −
System.Exception: First exception! ---< System.Exception: Middle Exception! ---< System.Exception: Last exception! at Demo.Two () [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0 --- End of inner exception stack trace --- at Demo.Two () [0x00016] in <199744cb72714131b4f5995ddd1a021f>:0 at Demo.One () [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0 --- End of inner exception stack trace --- at Demo.One () [0x00016] in <199744cb72714131b4f5995ddd1a021f>:0 at Demo.Main (System.String[] args) [0x00000] in <199744cb72714131b4f5995ddd1a021f>:0
- Related Articles
- What are chained exceptions in Java?
- Chained exception in Java\n
- Built-in Exceptions in C#
- Creating a chained operation class in JavaScript
- Checked vs Unchecked Exceptions in C#
- How do exceptions work in C++
- What are the custom exceptions in C#?
- What are user-defined exceptions in C#?
- User-defined Exceptions in C# with Example
- Throwing exceptions from C++ constructors
- How to catch all the exceptions in C++?
- How to create user defined exceptions in C#?
- Catching base and derived classes exceptions in C++
- Concrete Exceptions in Python
- Custom exceptions in Java

Advertisements