C Sharp (C#) - Exception Handling
You want to read a number from user input and handle errors if the input is not a number. Which code snippet correctly uses exception handling to do this?
try with int.Parse and catches FormatException, which is correct. int num = int.Parse(Console.ReadLine());
Console.WriteLine($"You entered {num}"); has no error handling. try {
int num = Console.ReadLine();
Console.WriteLine($"You entered {num}");
} catch (Exception) {
Console.WriteLine("Error occurred.");
} tries to assign string to int without parsing. try {
int num = Convert.ToInt32(Console.ReadLine());
} finally {
Console.WriteLine("Input processed.");
} uses finally but no catch, so errors are not handled.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions