开发者

Is the order of Catch blocks important?

开发者 https://www.devze.com 2023-02-02 14:42 出处:网络
Just making sure I understand it well. Is the correct schema correct? Ca开发者_运维知识库tching the most specific exceptions first to catching broader exceptions with general catch at the end of the s

Just making sure I understand it well. Is the correct schema correct? Ca开发者_运维知识库tching 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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消