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

Why exception handling is needed in C Sharp (C#) - Test Your Understanding

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

Complete the code to catch an exception.

C Sharp (C#)
try {
    int result = 10 / [1];
} catch (DivideByZeroException) {
    Console.WriteLine("Cannot divide by zero.");
}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-zero number does not cause an exception.
2fill in blank
medium

Complete the code to throw an exception when input is invalid.

C Sharp (C#)
if (age < 0) {
    throw new [1]("Age cannot be negative.");
}
Drag options to blanks, or click blank then click option'
AArgumentException
BArgumentNullException
CInvalidOperationException
DDivideByZeroException
Attempts:
3 left
💡 Hint
Common Mistakes
Using exceptions unrelated to argument errors.
3fill in blank
hard

Fix the error in the catch block to handle any exception.

C Sharp (C#)
try {
    int[] numbers = new int[3];
    int x = numbers[5];
} catch ([1]) {
    Console.WriteLine("An error occurred.");
}
Drag options to blanks, or click blank then click option'
AIndexOutOfRangeException
BNullReferenceException
CDivideByZeroException
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching only specific exceptions misses others.
4fill in blank
hard

Fill both blanks to handle a file error and display a message.

C Sharp (C#)
try {
    var text = System.IO.File.ReadAllText([1]);
} catch ([2]) {
    Console.WriteLine("File not found.");
}
Drag options to blanks, or click blank then click option'
A"data.txt"
BFileNotFoundException
CIOException
D"input.txt"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception type or missing quotes around filename.
5fill in blank
hard

Fill all three blanks to catch an exception and print its message.

C Sharp (C#)
try {
    int num = int.Parse([1]);
} catch ([2] ex) {
    Console.WriteLine([3]);
}
Drag options to blanks, or click blank then click option'
A"abc"
BFormatException
Cex.Message
D"123"
Attempts:
3 left
💡 Hint
Common Mistakes
Parsing a valid number does not cause exception.