Prasanth
Prasanth

Reputation: 75

Is it possible to conditionally execute a statement if there is no exception?

Consider the following function. I have written it in C#.

public void Test()
{
    statement1;
    try
    {
        statement2;
    }
    catch (Exception)
    {
        //Exception caught
    }
}

I want to execute statement1 only if statement2 causes no exception. Is it possible to execute statement1 only when statement2 doesn't throw any exception?

Upvotes: 1

Views: 950

Answers (7)

Salah Akbari
Salah Akbari

Reputation: 39966

If I've understood your question correctly, this is what you want (moving statement1; under statement2;):

try
{
    statement2;
    statement1;
}
catch (Exception)
{
    //Exception caught
}

By using this way, the statement1 will be executed only if statement2 causes no exception!

Upvotes: 3

Alessandro R
Alessandro R

Reputation: 635

Yes you can easily do it in this way

public void Test()
{
    try
    {
        statement2;
        statement1;
    }
    catch (Exception)
    {
        //Exception caught
    }
}

statement1 will not run if statement2 throws some exceptions.

Another way, not so cool, would be to use a variable

public void Test()
    {
        bool completed=false;
        try
        {
            statement2;
            completed=true;
        }
        catch (Exception)
        {
            //Exception caught
        }
        if (completed)
          statement1;
    }

Upvotes: 5

Gabriel Stancu
Gabriel Stancu

Reputation: 4260

Yes you can, all you have to do is to move statement 1 under statement 2, since the compiler will reach statement1 only if statement 2 did not throw any exception. Code below:

 public void Test()
{
try
{
    statement2;
    statement1;
}
catch (Exception)
{
    //Exception caught
}
}

Upvotes: 3

Anup Patil
Anup Patil

Reputation: 209

You can recall the method after the exception ,

public void Test(bool noerror= false)
{
   if (noerror)
      statement1;

    try
    {
        statement2;
        completed=true;
    }
    catch (Exception)
    {
         noerror=true;
        Test(noerror)
        //Exception caught
    }

}

Upvotes: 2

Dileep Namburu
Dileep Namburu

Reputation: 161

You can opt to recursion, but we need to make sure it doesn't end up as infinity loop.

public void Test()
{
   bool hasException = false;
   statement1;
   try
   {
       statement2;
   }
   catch (Exception)
   {
       hasException = true;
   }
   finally
   {
       if(hasException)
           Test();
   }
}

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37377

Yes there is and you are actually using it (but wrong).

try...catch block is meant to catch exceptions and take apropriate actions whether exception was thrown or not:

try
{
    // risky operation that might throw exception
    RiskyOperation();
    // here code is executed when no exception is thrown
}
catch(Exception ex)
{
    // code here gets executed when exception is thrown
}
finally
{
    // this code evaluates independently of exception occuring or not
}

To sum up, you need to do:

try
{
    statement2;
    statement1;
}
catch(Exception ex)
{
    // code here gets executed when exception is thrown
}

Upvotes: 1

A. Roussos
A. Roussos

Reputation: 258

Change the order and logic of statements. You cannot foresee an exception during runtime

Upvotes: 3

Related Questions