0
0
C Sharp (C#)programming~5 mins

Multiple catch blocks in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of multiple catch blocks in C#?
Multiple catch blocks allow a program to handle different types of exceptions separately, providing specific responses for each error type.
Click to reveal answer
beginner
How does C# decide which catch block to execute when an exception occurs?
C# checks each catch block in order and executes the first one that matches the type of the thrown exception or its base types.
Click to reveal answer
intermediate
Can you have a general catch block after specific ones? Why?
Yes, a general catch block (catch without specifying an exception type) can be placed last to catch any exceptions not handled by previous specific catch blocks.
Click to reveal answer
intermediate
What happens if no catch block matches the thrown exception?
If no catch block matches, the exception propagates up the call stack, potentially causing the program to terminate if unhandled.
Click to reveal answer
beginner
Show a simple example of multiple catch blocks in C#.
try { int[] numbers = {1, 2, 3}; Console.WriteLine(numbers[5]); } catch (IndexOutOfRangeException) { Console.WriteLine("Index is out of range."); } catch (Exception) { Console.WriteLine("Some other error occurred."); }
Click to reveal answer
What is the order of catch blocks when handling exceptions?
AFrom most general to most specific exception types
BAlphabetical order by exception name
CRandom order
DFrom most specific to most general exception types
What happens if a catch block for Exception is placed before a catch block for IndexOutOfRangeException?
AThe program will crash
BBoth catch blocks will execute
CThe IndexOutOfRangeException catch block will never be reached
DThe compiler will reorder the catch blocks automatically
Can you have multiple catch blocks for the same exception type?
AYes, but only if they have different variable names
BNo, each exception type can only appear once in catch blocks
CYes, catch blocks can be duplicated freely
DOnly if they are inside different try blocks
What is the role of a catch block without an exception type?
AIt catches any exception not caught by previous catch blocks
BIt catches only null reference exceptions
CIt is invalid syntax
DIt catches exceptions only in debug mode
If an exception is thrown but no catch block matches, what happens?
AThe exception propagates up the call stack
BThe program ignores the exception
CThe program automatically retries the operation
DThe exception is silently logged and discarded
Explain how multiple catch blocks work in C# and why their order matters.
Think about how you would catch different problems separately and why catching general problems first might cause issues.
You got /4 concepts.
    Write a simple C# try-catch example with at least two catch blocks for different exceptions.
    Use exceptions like IndexOutOfRangeException and Exception for general errors.
    You got /4 concepts.