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

Try-catch execution flow 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 exceptions.

C Sharp (C#)
try {
    int result = 10 / 0;
} catch ([1]) {
    Console.WriteLine("Error caught");
}
Drag options to blanks, or click blank then click option'
Aint
BException
Cstring
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-exception type like int or string in catch.
Leaving catch parentheses empty.
2fill in blank
medium

Complete the code to execute code regardless of exceptions.

C Sharp (C#)
try {
    Console.WriteLine("Try block");
} catch (Exception e) {
    Console.WriteLine("Catch block");
} [1] {
    Console.WriteLine("Finally block");
}
Drag options to blanks, or click blank then click option'
Aend
Bfinal
Cfinally
Dcleanup
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' or 'end' instead of 'finally'.
Omitting the finally block when cleanup is needed.
3fill in blank
hard

Fix the error in the catch block declaration.

C Sharp (C#)
try {
    int[] arr = new int[2];
    Console.WriteLine(arr[5]);
} catch ([1] e) {
    Console.WriteLine("Index error caught");
}
Drag options to blanks, or click blank then click option'
ANullReferenceException
BFormatException
CArgumentException
DIndexOutOfRangeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using NullReferenceException which is for null objects.
Using ArgumentException which is for invalid arguments.
4fill in blank
hard

Fill both blanks to catch a specific exception and print its message.

C Sharp (C#)
try {
    int x = int.Parse("abc");
} catch ([1] e) {
    Console.WriteLine(e.[2]);
}
Drag options to blanks, or click blank then click option'
AFormatException
BMessage
CStackTrace
DToString()
Attempts:
3 left
💡 Hint
Common Mistakes
Using StackTrace instead of Message for error text.
Catching the wrong exception type.
5fill in blank
hard

Fill all three blanks to handle division errors and print a custom message.

C Sharp (C#)
try {
    int a = 10, b = 0;
    int c = a [1] b;
} catch ([2] e) {
    Console.WriteLine("Cannot divide by zero: " + e.[3]);
}
Drag options to blanks, or click blank then click option'
A/
BDivideByZeroException
CMessage
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '/' for division.
Catching the wrong exception type.
Printing StackTrace instead of Message.