---
title: "Explain the difference between checked and unchecked exceptions in C#."  
description: "Explain the difference between checked and unchecked exceptions in C#."  
author: "Utpal Vishwas"  
published: 2023-06-04  
updated: 2023-11-18  
canonical: https://www.mindstick.com/forum/158620/explain-the-difference-between-checked-and-unchecked-exceptions-in-c-sharp  
category: "asp.net mvc"  
tags: ["asp.net", "exception handling", "asp.net mvc", "mvc"]  
reading_time: 3 minutes  

---

# Explain the difference between checked and unchecked exceptions in C#.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) between [checked and unchecked exceptions](https://www.mindstick.com/forum/159233/explain-the-difference-between-checked-and-unchecked-exceptions-in-java) in C#.

## Replies

### Reply by Aryan Kumar

In C#, [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) can be categorized into two types based on how the compiler handles them: checked exceptions and unchecked exceptions.

### Checked Exceptions:

## Compile-Time Checking:

- Checked exceptions are also known as compile-time exceptions. The compiler enforces that code must handle these exceptions or declare that it throws them.

## Use of checked Keyword:

- The **checked** keyword is used to explicitly enable the checking of arithmetic overflow and underflow conditions at runtime.

## Arithmetic Operations:

- Checked exceptions typically arise in scenarios involving arithmetic operations where the result exceeds the maximum or goes below the minimum value that can be represented by the data type.

## Example:

```plaintext
checked
{
    int result = int.MaxValue + 1;  // This will throw OverflowException
}
```

### Unchecked Exceptions:

## No Compile-Time Checking:

- Unchecked exceptions are not enforced by the compiler. The developer is responsible for handling or ignoring them appropriately.

## Use of unchecked Keyword:

- The **unchecked** keyword is used to suppress the checking of arithmetic overflow and underflow conditions at runtime.

## Arithmetic Operations:

- Unchecked exceptions often arise in the same scenarios as checked exceptions, but without the compiler enforcing checks.

## Example:

```plaintext
unchecked
{
    int result = int.MaxValue + 1;  // No exception will be thrown; the result will wrap around
}
```

### Key Differences:

## Handling:

- Checked exceptions require explicit handling or declaration using a **try-catch** block or **throws** clause. If not handled, the code won't compile.
- Unchecked exceptions do not require explicit handling, and the compiler does not enforce it. It's the developer's responsibility to handle them appropriately.

## Keywords:

- Checked exceptions use the **checked** keyword to enable runtime checking for arithmetic overflow and underflow.
- Unchecked exceptions use the **unchecked** keyword to suppress runtime checking for arithmetic overflow and underflow.

## Compiler Enforcement:

- Checked exceptions are enforced by the compiler, ensuring that the code explicitly handles them.
- Unchecked exceptions are not enforced by the compiler, allowing developers to choose whether to handle them or not.

## Default Behavior:

- By default, arithmetic operations in C# are unchecked. The **checked** and **unchecked** keywords are used to alter this default behavior.

Here's a simple example to illustrate the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is):

```plaintext
// Checked exception - compilation error if not handled
try
{
    checked
    {
        int result = int.MaxValue + 1;  // This will throw OverflowException if not handled
    }
}
catch (OverflowException ex)
{
    Console.WriteLine($"OverflowException: {ex.Message}");
}

// Unchecked exception - no compilation error
unchecked
{
    int result = int.MaxValue + 1;  // No compilation error, result will wrap around
}
```

In summary, checked exceptions are enforced by the compiler, requiring explicit handling, while unchecked exceptions allow developers more flexibility but require manual handling. The use of **checked** and **unchecked** keywords provides control over how arithmetic overflow and underflow are handled at runtime.


---

Original Source: https://www.mindstick.com/forum/158620/explain-the-difference-between-checked-and-unchecked-exceptions-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
