Câu hỏi phỏng vấn C#
Câu hỏi

How is Exception Handling implemented in C#?

Câu trả lời

Exception Handling in C#

Exception handling in C# is implemented using a combination of keywords and constructs that allow you to handle and manage errors that may occur during program execution. Here's how it works:

1. try-catch blocks

The try block is used to enclose code that may potentially throw an exception. If an exception occurs within the try block, the execution jumps to the corresponding catch block. Each catch block is designed to handle a specific type of exception.

csharp Copy
try
{
    // Code that may throw an exception
    int result = 10 / 0; // Throws DivideByZeroException
}
catch (DivideByZeroException ex)
{
    // Handle the exception
    Console.WriteLine($"Error: {ex.Message}");
}

2. Exception types

C# provides a hierarchy of exception types, derived from the base class System.Exception. This allows you to catch specific exceptions or handle them more generally.

csharp Copy
try
{
    // Code that may throw an exception
}
catch (ArgumentNullException ex)
{
    // Handle ArgumentNullException
}
catch (Exception ex)
{
    // Handle any other exception
}

3. throw statement

The throw statement is used to expli...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is difference between constant and readonly ?

expert

What are the benefits of a Deferred Execution in LINQ?

expert

List some different ways for equality check in .NET

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào