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

Can multiple catch blocks be executed?

Câu trả lời

Can multiple catch blocks be executed in C#?

In C#, it is possible to have multiple catch blocks within a single try-catch statement. However, only one catch block will be executed, depending on the type of exception that is thrown.

Here's how it works:

  1. When an exception is thrown within the try block, the runtime checks the catch blocks in the order they appear.
  2. The first catch block that can handle the thrown exception will be executed, and the remaining catch blocks will be skipped.
  3. If none of the catch blocks can handle the exception, the exception will be propagated up the call stack until it is handled by an outer try-catch statement or by the default exception handler.

Here's an example:

csharp Copy
try
{
    // Some code that might throw an exception
    int result = 10 / 0; // Throws DivideByZeroException
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("Caught DivideByZeroExc...
junior

junior

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

middle

How can you prevent a class from overriding in C#?

expert

What is the use of static constructors?

junior

What are the different types of classes in C#?

Bình luận

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

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