Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98220

A while loop with Try Catch throws exceptions after loop ends

$
0
0

I am working on writing some web-tests using Nunit with selenium in C# I have made a method which results in some weird behavior.

The point of the method is to try some action.

If the action succeeds exit the method.

If the action throws an exception, wait 100 ms and try it again.

If the action has been tried more than some timeout integer, throw an timeout exception.

I also wanted to add an option to do some other action if action throws an exception.

This is the code:

    public void Tryfor(Action tryAct, Action catchAct = null, int timeout = 50)
    {
        while (true) //loops until error or exception
        {
            Console.WriteLine(timeout); //For debugging
            if (timeout-- < 0) throw new TimeoutException("Tryfor timed out"); //reduce timeout by 1, when it reaches 0 throw exception
            try
            {
                tryAct();
                break; // If the try action succeeds with no exception, exit loop
            }
            catch   //If any exception is throw in the try action 
            {
                catchAct?.Invoke(); //If an action has been passed in the catch action, evoke it, else do nothing
            }
            Thread.Sleep(100); //wait 100 ms and try the try action again
        }
    }

This works just fine mos of the time, but as mentioned it sometimes behaves in a weird way i cant really understand.

What often happens is that the method runs through, maybe fails 2 times before it succeeds. Then after succeeding, it throws all the errors it caught.

What happens more often is that it reaches the timeout, meaning it failed (in this case) 50 times. Then instead of just throwing the timeout error, it throws the 50 errors it should have caught.

My goal is of course just to make a method which does what i described, but i would really like to understand how and why it throws all those errors it caught.


Viewing all articles
Browse latest Browse all 98220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>