Версия для печати темы

Нажмите сюда для просмотра этой темы в обычном формате

Форум на CrossPlatform.RU _ С\С++ _ Задачка N6

Автор: LuckLess 11.4.2008, 11:55

хых
Компилим мозгом

int main()
   {
   try
      {
      throw 5;
      }
   catch (int i)
      {
      try
         {
         ++i;
         throw;
         }
      catch (int i)
         {
         std::cout << i;
         }
      }
   }


что вылезет?

Автор: LuckLess 11.4.2008, 11:57

блин.. как значек № рисовать...

Автор: ViGOur 11.4.2008, 12:00

Цитата(LuckLess @ 11.4.2008, 12:57) *
блин.. как значек № рисовать...
В русской раскладке 3 c шифтом. :)

Автор: archimed7592 11.4.2008, 12:48

5 вылезет... А что ещё может вылезти? :)

Автор: ViGOur 11.4.2008, 12:56

Ну мало ли что там может вылезти еще, например 6, или еще куда прынгет... :)

Автор: Andrew Selivanov 11.4.2008, 16:45

гарантированный abnormal termination?
трям) не угадал

Автор: archimed7592 11.4.2008, 16:50

Andrew Selivanov, неа :).

Автор: Andrew Selivanov 11.4.2008, 16:54

Цитата(archimed7592 @ 11.4.2008, 17:50) *
Andrew Selivanov, неа :).

блин, не посмотрел, что второй throw внутри try... пять будет, пять)

Автор: Tonal 11.4.2008, 20:27

А вот если в первом catch ловить по ссылке - будет таки 6. :)

Автор: archimed7592 11.4.2008, 22:08

Цитата
А вот если в первом catch ловить по ссылке - будет таки 6.

Действительно :).
http://www.kuzbass.ru:%38%3086/docs/isocpp/except.html#except.throw
Цитата
-3- A throw-expression initializes a temporary object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from ``array of T'' or ``function returning T'' to ``pointer to T'' or ``pointer to function returning T'', respectively. [Note: the temporary object created for a throw-expression that is a string literal is never of type char* or wchar_t*; that is, the special conversions for string literals from the types ``array of const char'' and ``array of const wchar_t'' to the types ``pointer to char'' and ``pointer to wchar_t'', respectively (conv.array), are never applied to a throw-expression. ] The temporary is used to initialize the variable named in the matching handler (except.handle). The type of the throw-expression shall not be an incomplete type, or a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*. Except for these restrictions and the restrictions on type matching mentioned in except.handle, the operand of throw is treated exactly as a function argument in a call (expr.call) or the operand of a return statement.

-4- The memory for the temporary copy of the exception being thrown is allocated in an unspecified way, except as noted in basic.stc.dynamic.allocation. The temporary persists as long as there is a handler being executed for that exception. In particular, if a handler exits by executing a throw; statement, that passes control to another handler for the same exception, so the temporary remains. When the last handler being executed for the exception exits by any means other than throw; the temporary object is destroyed and the implementation may deallocate the memory for the temporary object; any such deallocation is done in an unspecified way. The destruction occurs immediately after the destruction of the object declared in the exception-declaration in the handler.

http://www.kuzbass.ru:%38%3086/docs/isocpp/except.html#except.handle
Цитата
-19- When the handler declares a non-constant object, any changes to that object will not affect the temporary object that was initialized by execution of the throw-expression. When the handler declares a reference to a non-constant object, any changes to the referenced object are changes to the temporary object initialized when the throw-expression was executed and will have effect should that object be rethrown.

Автор: Tonal 12.4.2008, 18:25

Поэтому правило простое: ловить исключение по const& (кроме примитивных типов) чтобы не было сюрпризов.
А примитивные - лучше не бросать.

Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)