In C#, 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:
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:
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:
// 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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, exceptions can be categorized into two types based on how the compiler handles them: checked exceptions and unchecked exceptions.
Checked Exceptions:
Compile-Time Checking:
Use of checked Keyword:
Arithmetic Operations:
Example:
Unchecked Exceptions:
No Compile-Time Checking:
Use of unchecked Keyword:
Arithmetic Operations:
Example:
Key Differences:
Handling:
Keywords:
Compiler Enforcement:
Default Behavior:
Here's a simple example to illustrate the difference:
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.