1

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?

16
  • 3
    By moving statement1; under statement2;? Commented Sep 12, 2018 at 6:09
  • 1
    You want a time machine? Commented Sep 12, 2018 at 6:18
  • 2
    What is stopping you from doing so? S.Akbari's answer is correct from the problem you've described in your question. Commented Sep 12, 2018 at 6:20
  • 1
    You can set the timer or await for that.But,it's not good way so faar. Commented Sep 12, 2018 at 6:23
  • 2
    @Prasanth: Don't worry about that. If you need a PDF to work with before you can determine if adding those fields would be successful, then just make the PDF temporarily. You probably won't even notice those milliseconds. Commented Sep 12, 2018 at 6:29

7 Answers 7

5

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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
}
}

Comments

3

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!

Comments

2

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
    }

}

1 Comment

Wait what? What makes you believe that re-running the same code if an exception occurs will work at some point? Also where does completed come from?
1

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
}

Comments

1

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();
   }
}

4 Comments

Wait what? What makes you believe that re-running the same code if an exception occurs will work at some point?
:) well, depending upon the functional context, it is the programmer's duty to make sure at some threshold point program must exit.
Yeah, but as is, your code will very likely either run fine the first time, or end up in an infinite loop. I mean if statement2 throws an exception the first time, it will likely throw it too the second time and every time after that. ("Insanity: doing the same thing over and over again and expecting different results.")
There are many ways to exit from this, for example you can have a global variable say var RetryCount = 5; , increment this var on each recursive call and exit when it reaches 6. It all depends on the requirement and context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.