How can we fix an exception and rerun the exact function that threw the exception? For example, in Selenium if there is a inline popup window that appears randomly on the website, we may get a NoSuchElement exception. I want to catch that exception, fix it (by closing the popup window), and then rerun the exact line of the program that threw the exception. How can I do this?
Asked
Active
Viewed 60 times
0
-
1Use [`try`](https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html) and [`catch`](https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html) clauses. – Maroun Jan 18 '16 at 14:33
-
Yep, I guess my question is how to I rerun the line that errored in the catch clause? – user3180 Jan 18 '16 at 14:36
-
Why do you not catch the exception, fix whatever went wrong, and call the method again? – LordAnomander Jan 18 '16 at 14:37
-
Because I'm using the "catch" to fix randomly occurring popups by closing them. If I fix it on the current page/state of my Selenium workflow, then I rerun my entire Selenium process again, it won't work when I rerun the process unless I start at the offending line (the second time I start the process the popup will reappear). – user3180 Jan 18 '16 at 14:40
-
instead of catching the exception, the alternative would just be a simple if statement. if the popup is present, then close the popup. – ddavison Jan 18 '16 at 15:09
-
1if its not an option you cannot set if in every row of your code, i think you need to use design pattern for this case. (try to look at Observer pattern) – Leon Barkan Jan 18 '16 at 15:17