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:

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

senior

Can you create a function in C# which can accept varying number of arguments?

senior

What is scope of a Protected Internal member variable of a C# class?

senior

What is Marshalling and why do we need it?

Bình luận

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

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