Complete the code to catch an exception.
try { int result = 10 / [1]; } catch (DivideByZeroException) { Console.WriteLine("Cannot divide by zero."); }
Dividing by zero causes an exception. Catching it prevents the program from crashing.
Complete the code to throw an exception when input is invalid.
if (age < 0) { throw new [1]("Age cannot be negative."); }
ArgumentException is used when an argument is invalid, like a negative age.
Fix the error in the catch block to handle any exception.
try { int[] numbers = new int[3]; int x = numbers[5]; } catch ([1]) { Console.WriteLine("An error occurred."); }
Catching Exception handles all exceptions, including unexpected ones.
Fill both blanks to handle a file error and display a message.
try { var text = System.IO.File.ReadAllText([1]); } catch ([2]) { Console.WriteLine("File not found."); }
Reading "data.txt" may cause FileNotFoundException if the file is missing.
Fill all three blanks to catch an exception and print its message.
try { int num = int.Parse([1]); } catch ([2] ex) { Console.WriteLine([3]); }
Parsing "abc" causes FormatException. Printing ex.Message shows the error.
