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

Throw and rethrow patterns 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 rethrow the caught exception preserving the original stack trace.

C Sharp (C#)
try {
    // Some code
} catch (Exception ex) {
    [1];
}
Drag options to blanks, or click blank then click option'
Athrow ex
Bthrow new Exception()
Cthrow
Dthrow ex.InnerException
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throw ex' which resets the stack trace.
Throwing a new exception loses original exception details.
2fill in blank
medium

Complete the code to throw a new exception with a custom message inside the catch block.

C Sharp (C#)
try {
    // Some code
} catch (Exception ex) {
    throw new Exception([1], ex);
}
Drag options to blanks, or click blank then click option'
Aex.Message
Bnull
Cex
D"Error occurred"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the exception object instead of a message string.
Using null instead of a descriptive message.
3fill in blank
hard

Fix the error in the catch block to correctly rethrow the exception preserving the stack trace.

C Sharp (C#)
try {
    // Some code
} catch (Exception ex) {
    [1];
}
Drag options to blanks, or click blank then click option'
Athrow
Bthrow new Exception(ex.Message)
Cthrow ex
Dthrow ex.InnerException
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throw ex' which resets the stack trace.
Throwing a new exception loses original stack trace.
4fill in blank
hard

Fill both blanks to create a try-catch that logs the exception message and then rethrows it preserving the stack trace.

C Sharp (C#)
try {
    // Some code
} catch (Exception ex) {
    Console.[1](ex.Message);
    [2];
}
Drag options to blanks, or click blank then click option'
AWriteLine
BWrite
Cthrow
Dthrow ex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throw ex' which resets the stack trace.
Using Console.Write instead of WriteLine for logging.
5fill in blank
hard

Fill all three blanks to catch an exception, wrap it in a new exception with a custom message, and throw it.

C Sharp (C#)
try {
    // Some code
} catch (Exception [1]) {
    throw new Exception([2], [3]);
}
Drag options to blanks, or click blank then click option'
Aex
B"Custom error occurred"
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than declared in catch.
Passing the wrong type as the message argument.
Not passing the original exception as inner exception.