can I nest try/catch blocks? 
Author Message
 can I nest try/catch blocks?

I haven't worked with exceptions too much in the past, so I not very
familiar with them. However, now I am writing a library and I want it to be
as stable as possible so I guess I need to catch the exceptions.

I have the following code:

CFile m_File;

if (m_File.Open(....))
{
     m_File.Seek(...);

Quote:
}

Now the above code is somewhat "unstable" since Seek can throw an exception,
so I decide to convert it to the following:

CFile m_File;

if (m_File.Open(...))
{
    try
    {
        m_File.Seek(...);
    }
    catch (CFileException* e)
    {
        m_File.Close();
        e->Delete();
    }

Quote:
}

Now that is a bit more stable, but now it is also possible for
CFile::Close() to throw an exception. Do I need to do another try block
inside of the catch (with a new variable?) or will the following work?

if (m_File.Open(...))
{
    try
    {
        m_File.Seek(...);
    }
    catch (CFileException* e)
    {
        e->Delete();
        try
        {
        m_File.Close();
        }
        catch (e)
        {
            e->Delete();
        }
    }

Quote:
}

is the scope of e ok here? Will this code cause any problems? Basically what
I want to do is open a file, and if it opened ok, seek to a certain point,
but if not, I want to make sure the m_hFile member of CFile is set to null
so later on I can check if the file is opened ok.

Gil Rosin



Fri, 18 Feb 2000 03:00:00 GMT  
 can I nest try/catch blocks?

You may nest try...catch blocks if you wish or you may place multiple catch
blocks for a single try.  MFC functions have exceptions specifications
which declare the exceptions they may throw.  It is reasonable (though
perhaps not always wise) to only attempt to catch the specified exceptions.

In the case of your sample code, in the event of an exception I would
suggest you call the CFile::Abort method.  This method closes the file
without throwing exceptions.  I think it is made for just your case.

Joe O'



Quote:
> I haven't worked with exceptions too much in the past, so I not very
> familiar with them. However, now I am writing a library and I want it to
be
> as stable as possible so I guess I need to catch the exceptions.

> Now that is a bit more stable, but now it is also possible for
> CFile::Close() to throw an exception. Do I need to do another try block
> inside of the catch (with a new variable?) or will the following work?

> if (m_File.Open(...))
> {
>     try
>     {
>         m_File.Seek(...);
>     }
>     catch (CFileException* e)
>     {
>         e->Delete();
>         try
>         {
>         m_File.Close();
>         }
>         catch (e)
>         {
>             e->Delete();
>         }
>     }
> }



Sat, 19 Feb 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. nested try/catch block and _set_se_translator

2. The if ~ else block in try~catch block

3. Nesting try, catch, finally

4. TRY-CATCH vs. try-catch

5. try/catch VS TRY/CATCH

6. Optimizer ignores try-catch block

7. try catch block

8. does atl support try..catch block?(empty inside)

9. Overhead of try-catch blocks?

10. /Og causes intermittent omission of try..catch block

11. try{} catch{} block and Global Optimization

12. Error processing using try-catch blocks

 

 
Powered by phpBB® Forum Software