0

I would like to try TWO different things (both have a strong possibility of failure) and for this reason I would like to use the "finally" statement to run a "safety" just in case the first two tries both fail.

Take the following example (no this is not the code I am using in my project!).

int zero = 0;
int one = 1;

try
{
    // Throws ' cannot divide by zero ' error
    int error = one / zero;
}

catch
{
    // Throws error again of course
    int somenum = one / zero;
}

finally 
{
    MessageBox.Show("I can never make it here ..."); 
}

So, I would like for my program to do the following:

  1. Attempt to divide by zero
  2. If step #1 fails, I would like for the 'catch' statement to run its code (which should once again fail in this example).
  3. IF both steps #1 and #2 fail, I would like for my program to display the MessageBox in the 'finally' statement.

Am I even close with this one?

user7116
  • 63,008
  • 17
  • 141
  • 172
  • 1
    Know that `finally` runs either way, success or failure. It's not like a second attempt at a `catch`, it's just going to run (unless the whole process is taken down). – Anthony Pegram Jul 02 '11 at 04:02

5 Answers5

5
int zero = 0;
int one = 1;

try {
    try
    {
        // Throws ' cannot divide by zero ' error
        int error = one / zero;
    }

    catch (DivideByZeroException)
    {
        // Throws error again of course
        int somenum = one / zero;
    }
}
catch (DivideByZeroException)
{
    MessageBox.Show("I can never make it here ...");
}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • one quibble: you should catch a specific error. Catching everything including threadabort and outofmemory is a bad idea – Conrad Frix Jul 02 '11 at 04:03
0

What you are doing will work. but note that after the message displayed in the finally block the exception will re-throw.

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • My code never makes it to the finally sector, though. After the second exception is thrown in the catch block the program just stops. –  Jul 02 '11 at 03:58
  • @Evan: no, your application is getting there but you are not notice it. To prove that run the exe file from explorer not from the VS. – Jalal Said Jul 02 '11 at 04:03
0

Inbest practice you should not place a code in catch that possible of failure.

for you sample you should use nested try-catch-finally

            int zero = 0;              
            int one = 1;
            bool flag=false;                
            try              
            {     
                try
                {
                    int error = one / zero; 
                }
                catch
                {
                    flag=false;
                }
                if(flag==false)               
                   int error = one / zero;              
            }                
            catch              
            {                  
                MessageBox.Show("I can never make it here ...");        
            } 

use finally section for free your resource and when you want after try or catch your code will executes 100%.

Arian
  • 12,793
  • 66
  • 176
  • 300
0

From my experience it seems you would need to put another try inside of your first catch:

        int zero = 0;
        int one = 1;

        try
        {
            // Throws ' cannot divide by zero ' error
            int error = one / zero;
        }

        catch
        {
            try
            {
                 // Throws error again of course
                 int somenum = one / zero;
            }
            catch { }
        }

        finally 
        {
            MessageBox.Show("I can never make it here ..."); 
        }
Brian Tacker
  • 1,091
  • 2
  • 18
  • 37
0

The reason of that is because you generate an exception on the catch block and don`t handle it.

try
{
    int error = one / zero; // throws an exception
}
catch // catches
{
    int somenum = one / zero; // throws again
}
// no code to handle the exception
// application crashes

You can avoid that placing a nested try catch block.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452