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

When clause in catch 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 only ArgumentNullException exceptions.

C Sharp (C#)
try {
    string s = null;
    Console.WriteLine(s.Length);
} catch (ArgumentNullException [1]) {
    Console.WriteLine("Null argument caught.");
}
Drag options to blanks, or click blank then click option'
Awhen (false)
Bwhen (true)
Cwhen (ex != null)
Dwhen (ex is ArgumentNullException)
Attempts:
3 left
💡 Hint
Common Mistakes
Using when (false) which never catches exceptions.
2fill in blank
medium

Complete the code to catch exceptions only when the message contains 'timeout'.

C Sharp (C#)
try {
    // some code
} catch (Exception ex [1] ex.Message.Contains("timeout")) {
    Console.WriteLine("Timeout error caught.");
}
Drag options to blanks, or click blank then click option'
Awhen
Bif
Ccatch
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of when in catch.
3fill in blank
hard

Fix the error in the catch block to properly use a when clause.

C Sharp (C#)
try {
    // code
} catch (Exception ex [1] ex.Message == null) {
    Console.WriteLine("Message is null.");
}
Drag options to blanks, or click blank then click option'
Aif
Bwhere
Cwhen
Dcatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of when.
4fill in blank
hard

Fill both blanks to catch InvalidOperationException only when the error code is 404.

C Sharp (C#)
try {
    // code
} catch (InvalidOperationException ex [1] ex.HResult [2] 404) {
    Console.WriteLine("Not found error caught.");
}
Drag options to blanks, or click blank then click option'
Awhen
B==
C!=
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of when.
Using != instead of ==.
5fill in blank
hard

Fill all three blanks to catch exceptions when the message contains 'disk' and the error code is 1234.

C Sharp (C#)
try {
    // code
} catch (Exception ex [1] ex.Message.Contains("disk") && ex.HResult [2] [3]) {
    Console.WriteLine("Disk error caught.");
}
Drag options to blanks, or click blank then click option'
Awhen
B==
C1234
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using if instead of when.
Using wrong comparison operators.