Loj Loj - 3 days ago 7
C# Question

Is the order of Catch blocks important?

Just making sure I understand it well. Is the correct schema correct? Catching the most specific exceptions first to catching broader exceptions with general catch at the end of the set of catch blocks.

try
{
some code
}


catch(SomeSpecificException ex)
{
}
catch(LessSpecificException ex)
{
}
catch
{
//some general exception
}

Rob Rob
Answer

I believe it won't let you write it in the incorrect order.

This generates an error:

try
{
 throw new OutOfMemoryException();
}


catch(Exception ex)
{
    "B".Dump();
}
catch(OutOfMemoryException ex)
{
    "A".Dump();
}
Comments