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

Multiple catch blocks in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch a general exception.

C Sharp (C#)
try {
    int result = 10 / 0;
} catch ([1] e) {
    Console.WriteLine("Error occurred");
}
Drag options to blanks, or click blank then click option'
Astring
Bint
CException
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using a data type like int or string instead of Exception.
Missing the exception type in the catch block.
2fill in blank
medium

Complete the code to catch a specific exception type for division by zero.

C Sharp (C#)
try {
    int result = 10 / 0;
} catch ([1] e) {
    Console.WriteLine("Cannot divide by zero.");
}
Drag options to blanks, or click blank then click option'
AFormatException
BNullReferenceException
CIndexOutOfRangeException
DDivideByZeroException
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated exception types like NullReferenceException.
Catching the wrong exception type.
3fill in blank
hard

Fix the error in the catch block to catch multiple exceptions separately.

C Sharp (C#)
try {
    string text = null;
    Console.WriteLine(text.Length);
} catch ([1] e) {
    Console.WriteLine("Null reference error.");
} catch (Exception e) {
    Console.WriteLine("General error.");
}
Drag options to blanks, or click blank then click option'
AException
BNullReferenceException
CDivideByZeroException
DFormatException
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception in the first catch block instead of NullReferenceException.
Not ordering catch blocks from specific to general.
4fill in blank
hard

Fill both blanks to catch specific exceptions and a general exception.

C Sharp (C#)
try {
    int[] numbers = {1, 2, 3};
    Console.WriteLine(numbers[5]);
} catch ([1] e) {
    Console.WriteLine("Index error.");
} catch ([2] e) {
    Console.WriteLine("Other error.");
}
Drag options to blanks, or click blank then click option'
AIndexOutOfRangeException
BDivideByZeroException
CException
DNullReferenceException
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of catch blocks.
Using unrelated exception types.
5fill in blank
hard

Fill all three blanks to catch multiple exceptions and print their messages.

C Sharp (C#)
try {
    string input = null;
    int length = input.Length;
    int result = 10 / 0;
} catch ([1] e) {
    Console.WriteLine("Null error: " + e.Message);
} catch ([2] e) {
    Console.WriteLine("Divide error: " + e.Message);
} catch ([3] e) {
    Console.WriteLine("General error: " + e.Message);
}
Drag options to blanks, or click blank then click option'
ANullReferenceException
BDivideByZeroException
CException
DIndexOutOfRangeException
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect order of catch blocks.
Using wrong exception types for the errors.