C Sharp (C#) - Exception Handling
Find the problem in this code:
try {
// code
} catch (Exception ex) when (ex.Message == null) {
Console.WriteLine("Null message");
} catch {
Console.WriteLine("General catch");
}Find the problem in this code:
try {
// code
} catch (Exception ex) when (ex.Message == null) {
Console.WriteLine("Null message");
} catch {
Console.WriteLine("General catch");
}ex.Message == null accesses ex.Message, which is never null but could be empty. However, if ex was null (which it cannot be), it would cause an error.ex.Message is safe, but if the condition was more complex and accessed properties without null checks, it could cause NullReferenceException.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions