URL: https://www.opennet.me/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 927
[ Назад ]

Исходное сообщение
"try/finally in gcc"

Отправлено romio , 24-Авг-02 21:15 
что можно поюзать вместо субжа?
всё бы ничего, но из блока try {} бывают return'ы и надо как-то обеспечить finally.

Содержание

Сообщения в этом обсуждении
"RE: try/finally in gcc"
Отправлено sas , 24-Авг-02 22:16 
>что можно поюзать вместо субжа?
>всё бы ничего, но из блока try {} бывают return'ы и надо
>как-то обеспечить finally.

Hi,

This is C++ not java, so

public:
void m1()
{
    try {
        // no returns and gotos from try block
        if ( i_want_to_call_return ) {
            finally();
            return;
        }
    }
    catch ( ... ) { // will catch any exception
    }
    finally();
}

private:
void finally()
{
    // your finally stuff
}

Thanks
--- Sas

:)



"RE: try/finally in gcc"
Отправлено LS , 25-Авг-02 23:30 
>>что можно поюзать вместо субжа?
>>всё бы ничего, но из блока try {} бывают return'ы и надо
>>как-то обеспечить finally.
>
>Hi,
>
>This is C++ not java, so
>
>public:
>void m1()
>{
> try {
>  // no returns and gotos from try block
>  if ( i_want_to_call_return ) {
>   finally();
>   return;
>  }
> }
> catch ( ... ) { // will catch any exception
> }
> finally();
>}
>
>private:
>void finally()
>{
> // your finally stuff
>}
>
>Thanks
>--- Sas
>
>:)


Или как вариант:

void xxx()
{
try
{
  try
  {
    bla;
    bla;
    bla;
   // вместо return генерируем искючение
   excetp ...
  }
  catch
  {
   //обрабатываем ошибки внутри функции (все, кроме ...)
  }
}
catch ...
{
  // обрабатываем except ... = finally
}
}

PS С основательно подзабыл, но думаю идея понятна.


"RE: try/finally in gcc"
Отправлено sas , 25-Авг-02 23:43 
Hi LS,

Your solution is really better!

Thanks
--- Sas


"RE: try/finally in gcc"
Отправлено sas , 25-Авг-02 23:54 
Hi,

LS's variant with try { try {} } is better.

Alos some corrections to code I have posted previously.

public:
void m1()
{
try {
    // no returns and gotos from try block
    if ( i_want_to_call_return ) {
        finally();
        return;
    }
}
catch ( const some_exception& e ) {
        finally(); // put finally if you need transparent exceptions
        throw;
}
catch ( const some_other_exception& oe ) {
        // no finally if we are not rethrow
}
catch ( ... ) { // will catch any exception
    finally();
    throw;
}
finally();
}


private:
void finally()
{
// your finally stuff
}


"RE: try/finally in gcc"
Отправлено genie , 26-Авг-02 03:10 
>что можно поюзать вместо субжа?
>всё бы ничего, но из блока try {} бывают return'ы и надо
>как-то обеспечить finally.

A mozhet prosche ispol'zovat' destructory ob'ektov? Primerno tak:

class TEMP
{
  public:
     ~TEMP()
     {
        //Do smth here
     }
};


void function()
{
    TEMP t;

    //bla-bla-bla
}

Kogda function vozvraschaetsya, budet vyzvan destructor.