From [except.throw]/2:
When an exception is thrown, control is transferred to the nearest handler with a matching type ([except.handle]); “nearest” means the handler for which the compound-statement or ctor-initializer following the try keyword was most recently entered by the thread of control and not yet exited.
First of all, I really can't understand this quote properly, and how it is applied in a particular code. So any understandable explanations, with simple words, will be very appreciated.
Consider this two code snippets:
/ * code A */
try
{
throw 1;
}
catch (int) // nearest handler for throwing 1. #first_handler
{
}
-----------------------------------------------
/* code B */
try
{
try
{
throw 1;
}
catch (int) // nearest handler for throwing 1. #second_handler
{
throw 2;
}
}
catch (int) // nearest handler for throwing 2
{
}
Because I cannot understand the above quote, I don't know how I can determine the nearest handler, and I cannot apply [except.throw]/2 in the above examples. My question is just how does [except.throw]/2 is applied in the above code? or in other words, why first_handler and second_handlerare nearest handlers?