---
title: "Throwing multiple exceptions in .Net or C#"  
description: "Throwing multiple exceptions in .Net or C#"  
author: "Revati S Misra"  
published: 2023-07-20  
updated: 2023-07-20  
canonical: https://www.mindstick.com/forum/159179/throwing-multiple-exceptions-in-dot-net-or-c-sharp  
category: ".net"  
tags: ["c#", ".net", "exception"]  
reading_time: 2 minutes  

---

# Throwing multiple exceptions in .Net or C#

Throwing [multiple exceptions](https://www.mindstick.com/forum/159240/how-to-handle-multiple-exceptions-using-the-catch-block-in-java) in .Net/C#

## Replies

### Reply by Aryan Kumar

Sure, here are the ways to throw [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) in .NET and C#:

- **Using the** `throw` **statement:** The `throw` statement is used to throw an exception. You can throw any exception that you want, including multiple exceptions. To throw multiple exceptions, you can use the `throw` statement multiple times. For example, the following code throws two exceptions:

C#

```plaintext
try {
  // This code might raise an exception
  throw new Exception("This is the first exception.");
} catch (Exception e) {
  throw new Exception("This is the second exception.");
}
```

- **Using the** `AggregateException` **class:** The `AggregateException` class is a class that represents a collection of exceptions. You can use the `AggregateException` class to throw multiple exceptions at once. To do this, you can create an instance of the `AggregateException` class and then add the exceptions that you want to throw to the `AggregateException` object. For example, the following code throws two exceptions using the `AggregateException` class:

C#

```plaintext
try {
  // This code might raise an exception
  throw new Exception("This is the first exception.");
} catch (Exception e) {
  AggregateException ae = new AggregateException();
  ae.Add(new Exception("This is the second exception."));
  throw ae;
}
```

- **Using the** `when` **keyword:** The `when` keyword is a new feature in C# 8 that allows you to throw multiple exceptions based on a condition. To use the `when` keyword, you need to wrap the code that you want to execute in a `try` block. Then, you need to use the `when` keyword to specify the condition that you want to check. If the condition is true, the code in the `try` block will be executed. Otherwise, the exception that you specify in the `when` clause will be thrown. For example, the following code throws an `ArgumentNullException` exception if the string argument is null:

C#

```plaintext
try {
  string str = null;
  // This code might raise an exception
  throw new Exception("This is the first exception.");
} catch (Exception e) {
  throw new ArgumentNullException("str", "The string argument cannot be null.");
}
```


---

Original Source: https://www.mindstick.com/forum/159179/throwing-multiple-exceptions-in-dot-net-or-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
