简体   繁体   中英

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
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM