Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
try
{
Console.WriteLine(int.Parse("ten"));
}
catch (Exception)
{
Console.WriteLine("general");
}
catch (FormatException)
{
Console.WriteLine("format");
}It does not compile
- Expecting
general: at run time blocks are tried top to bottom, but a generalcatchabove a specific one is compile errorCS0160, so the program never runs. - Expecting the most specific
catchto win wherever it is written: the order matters, so specific classes go abovecatch (Exception).
- How would you reorder the blocks so that the program prints
format? - What would the program print with only the
catch (Exception)block left, and why?