Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-exception type like int or string in catch.
Leaving catch parentheses empty.
✗ Incorrect
The catch block must specify an exception type like Exception to catch errors.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'final' or 'end' instead of 'finally'.
Omitting the finally block when cleanup is needed.
✗ Incorrect
The finally block runs code no matter if an exception occurs or not.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NullReferenceException which is for null objects.
Using ArgumentException which is for invalid arguments.
✗ Incorrect
Accessing an invalid array index throws IndexOutOfRangeException.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using StackTrace instead of Message for error text.
Catching the wrong exception type.
✗ Incorrect
FormatException is thrown by int.Parse on invalid input; e.Message shows the error text.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '/' for division.
Catching the wrong exception type.
Printing StackTrace instead of Message.
✗ Incorrect
Division uses '/', division by zero throws DivideByZeroException, and Message shows the error.