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

Exception handling in async code 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 in an async method.

C Sharp (C#)
try
{
    await SomeAsyncMethod();
}
catch ([1] ex)
{
    Console.WriteLine(ex.Message);
}
Drag options to blanks, or click blank then click option'
Aint
BException
Cstring
DTask
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-exception type in the catch block.
Not using await before the async method.
2fill in blank
medium

Complete the code to rethrow the caught exception preserving the stack trace.

C Sharp (C#)
try
{
    await SomeAsyncMethod();
}
catch (Exception ex)
{
    [1];
}
Drag options to blanks, or click blank then click option'
Athrow new Exception()
Bthrow ex
Creturn
Dthrow
Attempts:
3 left
💡 Hint
Common Mistakes
Using throw ex; which loses the original stack trace.
Using return instead of throwing.
3fill in blank
hard

Fix the error in the async method signature to enable exception handling.

C Sharp (C#)
public async [1] SomeAsyncMethod()
{
    throw new Exception("Error");
}
Drag options to blanks, or click blank then click option'
ATask
Bvoid
Cint
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using void return type which prevents awaiting and catching exceptions.
Returning non-task types from async methods.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters exceptions by message length.

C Sharp (C#)
var errors = new Dictionary<string, string>();
foreach (var ex in exceptions)
{
    if (ex.Message.Length [1] 10)
    {
        errors.Add(ex.Message, ex[2]);
    }
}
Drag options to blanks, or click blank then click option'
A>
BMessage
C<
DInnerException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator.
Adding the wrong property as dictionary value.
5fill in blank
hard

Fill all three blanks to create a LINQ query that selects exception messages with inner exceptions.

C Sharp (C#)
var messages = exceptions.Where(ex => ex.[1] != null)
    .Select(ex => ex.[2])
    .Where(msg => msg.[3]("error", StringComparison.OrdinalIgnoreCase))
    .ToList();
Drag options to blanks, or click blank then click option'
AInnerException
BMessage
CContains
DStackTrace
Attempts:
3 left
💡 Hint
Common Mistakes
Using StackTrace instead of Message.
Checking wrong property for null.